Learner Level

Implement the leftRotateArrayByOne method that returns the array after rotating all elements one position to the left.

The input contains an integer array nums and its length size. Your task is to rotate the array one position to the left and return the updated array.

In a left rotation, the first element moves to the end and every other element shifts one position toward the front. For example, [1,2,3,4] becomes [2,3,4,1].

Example 1

Input
nums (int[]) = [1,2,3,4]
size (int) = 4
Returns (int[]) [2,3,4,1]

Example 2

Input
nums (int[]) = [9]
size (int) = 1
Returns (int[]) [9]

Example 3

Input
nums (int[]) = [5,6]
size (int) = 2
Returns (int[]) [6,5]

Handle arrays of size 0 or 1 directly because rotation will not change them.

For larger arrays, store the first element, shift every remaining value one position left, then place the stored first element at the last position.

Pseudocode:

function leftRotateArrayByOne(nums, size):
    if size <= 1:
        return nums
    first = nums[0]
    for i from 0 to size - 2:
        nums[i] = nums[i + 1]
    nums[size - 1] = first
    return nums
Ready to test your solutionRun Code checks your solution against the test cases.Shift + Enter to run

Notifications

Updates are enabled for this browser.

Receive new tutorials, articles and learning resources. You can turn updates off anytime.

Test browser code

Use this code in admin to send a test notification to this browser.