LearnWithKiriti

Left Rotate Array by One

Easy GitHub ↗
đź’» Try it yourself first. Write your solution in Java, C++, Python or JavaScript and run it against hidden tests, right here. New: mock interview with Mr. Tock. Explain your approach out loud, he pushes you to improve it, then you code it. You get a scorecard at the end.
Play
On this page
  1. Revision notes
  2. Brute: Temp Array
  3. Optimal

Revision notes

Left Rotate Array by One

Video: “Left Rotate Array by One: where did the 1 go?” on LearnWithKiriti (Kodee).

Move every number one step left, in place; the first number goes to the end. Example: [1, 2, 3, 4, 5] -> [2, 3, 4, 5, 1]

Folder Approach Time Extra space
Brute-TempArray/ Brute force: Copy everyone one step left into a new array, put the first number at the end, copy back. O(n) O(n) extra (a second array)
Optimal/ Optimal: Remember the first number, shift everyone one step left in place, then put the first number in the last spot. O(n) O(1)

Rebuild it

Save the first, shift everyone left, put the first at the end. Shifting overwrites nums[0] first, so remember it (first) before the loop.

See each folder’s pseudocode.md. Every program runs Example 1 and prints the answer; the same code is in the practice panel’s Start menu on learnwithkiriti.com, where Shelf Bot can act it out.

brute

Brute: Temp Array

Brute: a second array

Copy everyone one step left into a new array, put the first number at the end, copy back.

moved = new array of size
for scan from 1 to size - 1:
    moved[scan - 1] = nums[scan]
moved[size - 1] = nums[0]
copy moved back into nums

O(n) time, O(n) extra space for moved.

LeftRotateArrayByOneBrute.java
import java.util.*;

public class LeftRotateArrayByOneBrute {
    public void rotateByOne(int[] nums) {
        int size = nums.length;
        int[] moved = new int[size];
        for (int scan = 1; scan < size; scan++) {
            moved[scan - 1] = nums[scan];
        }
        moved[size - 1] = nums[0];
        for (int scan = 0; scan < size; scan++) {
            nums[scan] = moved[scan];
        }
    }

    public static void main(String[] args) {
        LeftRotateArrayByOneBrute solver = new LeftRotateArrayByOneBrute();
        int[] nums = new int[]{1, 2, 3, 4, 5};
        solver.rotateByOne(nums);
        System.out.println(Arrays.toString(nums));
    }
}
optimal

Optimal

Optimal: in place

Remember the first number, shift everyone one step left in place, then put the first number in the last spot.

first = nums[0]
for scan from 1 to size - 1:
    nums[scan - 1] = nums[scan]
nums[size - 1] = first

Dry run on Example 1:

scan array
start (first = 1) [1, 2, 3, 4, 5]
1 [2, 2, 3, 4, 5]
2 [2, 3, 3, 4, 5]
3 [2, 3, 4, 4, 5]
4 [2, 3, 4, 5, 5]
end [2, 3, 4, 5, 1]

O(n) time, O(1) extra space.

LeftRotateArrayByOneOptimal.java
import java.util.*;

public class LeftRotateArrayByOneOptimal {
    public void rotateByOne(int[] nums) {
        int first = nums[0];
        for (int scan = 1; scan < nums.length; scan++) {
            nums[scan - 1] = nums[scan];
        }
        nums[nums.length - 1] = first;
    }

    public static void main(String[] args) {
        LeftRotateArrayByOneOptimal solver = new LeftRotateArrayByOneOptimal();
        int[] nums = new int[]{1, 2, 3, 4, 5};
        solver.rotateByOne(nums);
        System.out.println(Arrays.toString(nums));
    }
}