Left Rotate Array by One
đź’» Try it yourself first. Write your solution in Java, C++, Python or JavaScript and run it against hidden tests, right here.On this page
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: 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.
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));
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
void rotateByOne(vector<int>& nums) {
int size = nums.size();
vector<int> moved(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];
}
}
};
void print(const vector<int>& values) {
cout << "[";
for (size_t spot = 0; spot < values.size(); spot++) {
if (spot > 0) {
cout << ", ";
}
cout << values[spot];
}
cout << "]" << endl;
}
void print(const vector<vector<int>>& grid) {
for (const vector<int>& line : grid) {
print(line);
}
}
int main() {
Solution solver;
vector<int> nums = {1, 2, 3, 4, 5};
solver.rotateByOne(nums);
print(nums);
return 0;
}def rotate_by_one(nums):
size = len(nums)
moved = [0] * size
for scan in range(1, size):
moved[scan - 1] = nums[scan]
moved[size - 1] = nums[0]
for scan in range(size):
nums[scan] = moved[scan]
if __name__ == "__main__":
nums = [1, 2, 3, 4, 5]
rotate_by_one(nums)
print(nums)function rotateByOne(nums) {
const size = nums.length;
const moved = new Array(size);
for (let scan = 1; scan < size; scan++) {
moved[scan - 1] = nums[scan];
}
moved[size - 1] = nums[0];
for (let scan = 0; scan < size; scan++) {
nums[scan] = moved[scan];
}
}
const nums = [1, 2, 3, 4, 5];
rotateByOne(nums);
console.log(JSON.stringify(nums).replace(/,/g, ', '));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.
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));
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
void rotateByOne(vector<int>& nums) {
int first = nums[0];
for (int scan = 1; scan < (int) nums.size(); scan++) {
nums[scan - 1] = nums[scan];
}
nums[nums.size() - 1] = first;
}
};
void print(const vector<int>& values) {
cout << "[";
for (size_t spot = 0; spot < values.size(); spot++) {
if (spot > 0) {
cout << ", ";
}
cout << values[spot];
}
cout << "]" << endl;
}
void print(const vector<vector<int>>& grid) {
for (const vector<int>& line : grid) {
print(line);
}
}
int main() {
Solution solver;
vector<int> nums = {1, 2, 3, 4, 5};
solver.rotateByOne(nums);
print(nums);
return 0;
}def rotate_by_one(nums):
first = nums[0]
for scan in range(1, len(nums)):
nums[scan - 1] = nums[scan]
nums[len(nums) - 1] = first
if __name__ == "__main__":
nums = [1, 2, 3, 4, 5]
rotate_by_one(nums)
print(nums)function rotateByOne(nums) {
const first = nums[0];
for (let scan = 1; scan < nums.length; scan++) {
nums[scan - 1] = nums[scan];
}
nums[nums.length - 1] = first;
}
const nums = [1, 2, 3, 4, 5];
rotateByOne(nums);
console.log(JSON.stringify(nums).replace(/,/g, ', '));