rotateLeft

Easy · Array Manipulations · 3 test cases

Given an array of ints, return an array with the elements 'rotated left' so [1, 2, 3] yields [2, 3, 1].

Examples

rotateLeft([1, 2, 3]) → [2, 3, 1]
rotateLeft([5, 11, 9]) → [11, 9, 5]

Starter code

function rotateLeft(nums) {

}

Complexity of the optimal solution

Time O(n), space O(1). The `shift()` operation on an array is O(n) because all subsequent elements need to be moved one position to the left. `push()` is O(1). The dominant factor is `shift()`, making the time complexity linear. The operation is done in-place, so space is constant.

Solve rotateLeft in the browser