Given an integer array nums and a non-negative integer k, rotate the array to the left by k steps.
Input: nums = [1, 2, 3, 4, 5, 6], k = 2
Output: nums = [3, 4, 5, 6, 1, 2]
Explanation: rotate 1 step to the left: [2, 3, 4, 5, 6, 1]
rotate 2 steps to the left: [3, 4, 5, 6, 1, 2]
Input: nums = [3, 4, 1, 5, 3, -5], k = 8
Output: nums = [1, 5, 3, -5, 3, 4]
Explanation: rotate 1 step to the left: [4, 1, 5, 3, -5, 3]
rotate 2 steps to the left: [1, 5, 3, -5, 3, 4]
rotate 3 steps to the left: [5, 3, -5, 3, 4, 1]
rotate 4 steps to the left: [3, -5, 3, 4, 1, 5]
rotate 5 steps to the left: [-5, 3, 4, 1, 5, 3]
rotate 6 steps to the left: [3, 4, 1, 5, 3, -5]
rotate 7 steps to the left: [4, 1, 5, 3, -5, 3]
rotate 8 steps to the left: [1, 5, 3, -5, 3, 4]
Input: nums = [1, 2, 3, 4, 5], k = 4
The most optimal solution works based on the properties of reversing sections of the array. This involves reversing different parts of the array to achieve the desired rotation.






#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
// Function to rotate the array to the left by k positions
void rotateArray(vector<int>& nums, int k) {
int n = nums.size(); // Size of array
k = k % n; // To avoid unnecessary rotations
vector<int> temp;
// Store first k elements in a temporary array
for(int i=0; i < k; i++) {
temp.push_back(nums[i]);
}
// Shift n-k elements of given array to the front
for(int i=k; i < n; i++) {
nums[i-k] = nums[i];
}
// Copy back the k elemnents at the end
for(int i=0; i < k; i++) {
nums[n-k+i] = temp[i];
}
}
};
// Helper function to print the array
void printArray(vector<int> nums) {
for(int val : nums) {
cout << val << " ";
}
cout << endl;
}
int main() {
vector nums = {1, 2, 3, 4, 5, 6};
int k = 2;
cout << "Initial array: ";
printArray(nums);
// Create an instance of the Solution class
Solution sol;
/* Function call to rotate the
array to the left by k places */
sol.rotateArray(nums, k);
cout << "Array after rotating elements by " << k << " places: ";
printArray(nums);
return 0;
}import java.util.*;
class Solution {
// Function to rotate the array to the left by k positions
public void rotateArray(int[] nums, int k) {
int n = nums.length; // Size of array
k = k % n; // To avoid unnecessary rotations
int[] temp = new int[k];
// Store first k elements in a temporary array
for (int i = 0; i < k; i++) {
temp[i] = nums[i];
}
// Shift n-k elements of given array to the front
for (int i = k; i < n; i++) {
nums[i - k] = nums[i];
}
// Copy back the k elements at the end
for (int i = 0; i < k; i++) {
nums[n - k + i] = temp[i];
}
}
}
class Main {
// Helper function to print the array
public static void printArray(int[] nums) {
for (int val : nums) {
System.out.print(val + " ");
}
System.out.println();
}
public static void main(String[] args) {
int[] nums = {1, 2, 3, 4, 5, 6};
int k = 2;
System.out.println("Initial array: ");
printArray(nums);
// Create an instance of the Solution class
Solution sol = new Solution();
/* Function call to rotate the
array to the left by k places */
sol.rotateArray(nums, k);
System.out.println("Array after rotating elements by " + k + " places: ");
printArray(nums);
}
}class Solution:
# Function to rotate the array to the left by k positions
def rotateArray(self, nums, k):
n = len(nums) # Size of array
k = k % n # To avoid unnecessary rotations
temp = []
# Store first k elements in a temporary array
for i in range(k):
temp.append(nums[i])
# Shift n-k elements of given array to the front
for i in range(k, n):
nums[i - k] = nums[i]
# Copy back the k elements at the end
for i in range(k):
nums[n - k + i] = temp[i]
# Helper function to print the array
def printArray(nums):
for val in nums:
print(val, end=" ")
print()
if __name__ == "__main__":
nums = [1, 2, 3, 4, 5, 6]
k = 2
print("Initial array: ")
printArray(nums)
# Create an instance of the Solution class
sol = Solution()
# Function call to rotate the array to the left by k places
sol.rotateArray(nums, k)
print(f"Array after rotating elements by {k} places: ")
printArray(nums)class Solution {
// Function to rotate the array to the left by k positions
rotateArray(nums, k) {
let n = nums.length; // Size of array
k = k % n; // To avoid unnecessary rotations
let temp = [];
// Store first k elements in a temporary array
for (let i = 0; i < k; i++) {
temp.push(nums[i]);
}
// Shift n-k elements of given array to the front
for (let i = k; i < n; i++) {
nums[i - k] = nums[i];
}
// Copy back the k elements at the end
for (let i = 0; i < k; i++) {
nums[n - k + i] = temp[i];
}
}
}
// Helper function to print the array
function printArray(nums) {
console.log(nums.join(' '));
}
const nums = [1, 2, 3, 4, 5, 6];
const k = 2;
console.log("Initial array: ");
printArray(nums);
// Create an instance of the Solution class
const sol = new Solution();
/* Function call to rotate the
array to the left by k places */
sol.rotateArray(nums, k);
console.log(`Array after rotating elements by ${k} places: `);
printArray(nums);The most optimal solution works based on the properties of reversing sections of the array. This involves reversing different parts of the array to achieve the desired rotation.






#include <bits/stdc++.h>
using namespace std;
class Solution {
private:
// Function to reverse the array between start and end
void reverseArray(vector<int>& nums, int start, int end) {
while (start < end) {
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++, end--;
}
}
public:
// Function to rotate the array to the left by k positions
void rotateArray(vector<int>& nums, int k) {
int n = nums.size(); // Size of array
k = k % n; // To avoid unnecessary rotations
// Reverse the first k elements
reverseArray(nums, 0, k - 1);
// Reverse the last n-k elements
reverseArray(nums, k, n - 1);
// Reverse the entire vector
reverseArray(nums, 0, n - 1);
}
};
// Helper function to print the array
void printArray(vector<int> nums) {
for(int val : nums) {
cout << val << " ";
}
cout << endl;
}
int main() {
vector nums = {1, 2, 3, 4, 5, 6};
int k = 2;
cout << "Initial array: ";
printArray(nums);
// Create an instance of the Solution class
Solution sol;
/* Function call to rotate the
array to the left by k places */
sol.rotateArray(nums, k);
cout << "Array after rotating elements by " << k << " places: ";
printArray(nums);
return 0;
}import java.util.*;
class Solution {
// Function to reverse the array between start and end
private void reverseArray(int[] nums, int start, int end) {
while (start < end) {
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
}
// Function to rotate the array to the left by k positions
public void rotateArray(int[] nums, int k) {
int n = nums.length; // Size of array
k = k % n; // To avoid unnecessary rotations
// Reverse the first k elements
reverseArray(nums, 0, k - 1);
// Reverse the last n-k elements
reverseArray(nums, k, n - 1);
// Reverse the entire array
reverseArray(nums, 0, n - 1);
}
}
class Main {
// Helper function to print the array
public static void printArray(int[] nums) {
for (int val : nums) {
System.out.print(val + " ");
}
System.out.println();
}
public static void main(String[] args) {
int[] nums = {1, 2, 3, 4, 5, 6};
int k = 2;
System.out.println("Initial array: ");
printArray(nums);
// Create an instance of the Solution class
Solution sol = new Solution();
/* Function call to rotate the
array to the left by k places */
sol.rotateArray(nums, k);
System.out.println("Array after rotating elements by " + k + " places: ");
printArray(nums);
}
}class Solution:
# Function to reverse the array between start and end
def reverseArray(self, nums, start, end):
while start < end:
temp = nums[start]
nums[start] = nums[end]
nums[end] = temp
start += 1
end -= 1
# Function to rotate the array to the left by k positions
def rotateArray(self, nums, k):
n = len(nums) # Size of array
k = k % n # To avoid unnecessary rotations
# Reverse the first k elements
self.reverseArray(nums, 0, k - 1)
# Reverse the last n-k elements
self.reverseArray(nums, k, n - 1)
# Reverse the entire array
self.reverseArray(nums, 0, n - 1)
# Helper function to print the array
def printArray(nums):
for val in nums:
print(val, end=" ")
print()
if __name__ == "__main__":
nums = [1, 2, 3, 4, 5, 6]
k = 2
print("Initial array: ")
printArray(nums)
# Create an instance of the Solution class
sol = Solution()
# Function call to rotate the array to the left by k places
sol.rotateArray(nums, k)
print(f"Array after rotating elements by {k} places: ")
printArray(nums)class Solution {
// Function to reverse the array between start and end
reverseArray(nums, start, end) {
while (start < end) {
let temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
start++;
end--;
}
}
// Function to rotate the array to the left by k positions
rotateArray(nums, k) {
let n = nums.length; // Size of array
k = k % n; // To avoid unnecessary rotations
// Reverse the first k elements
this.reverseArray(nums, 0, k - 1);
// Reverse the last n-k elements
this.reverseArray(nums, k, n - 1);
// Reverse the entire array
this.reverseArray(nums, 0, n - 1);
}
}
// Helper function to print the array
function printArray(nums) {
console.log(nums.join(" "));
}
const nums = [1, 2, 3, 4, 5, 6];
const k = 2;
console.log("Initial array: ");
printArray(nums);
// Create an instance of the Solution class
const sol = new Solution();
/* Function call to rotate the
array to the left by k places */
sol.rotateArray(nums, k);
console.log(`Array after rotating elements by ${k} places: `);
printArray(nums);Q: What if k is greater than or equal to the array length?
A: When k ≥ nums.length, the rotation repeats unnecessarily. To simplify: Reduce k to k % nums.length. This ensures that the rotation is performed only as much as necessary. Example: Input: nums = [1, 2, 3], k=4 Effective k=4%3=1. Output: [2, 3, 1].
Q: How can this be done in-place?
A: Instead of creating a new array: - Reverse the entire array. - Reverse the first n−k elements (left part). - Reverse the last k elements (right part). - This method achieves the same result in O(1) extra space.
Q: Can this logic be extended to multidimensional arrays?
A: Yes, but for 2D arrays (e.g., matrices), rotation involves more complex transformations: - For left rotation, each row/column is treated as a separate 1D array and rotated individually. - For k>1, the process must account for overlapping boundaries in 2D structures.
Q: What’s the difference between rotation by k and shuffling?
A: Rotation shifts elements in a structured, cyclic order, preserving their relative positions. Shuffling, on the other hand, rearranges elements randomly without preserving order. Rotation is deterministic, whereas shuffling introduces randomness.
Your notes are automatically saved in your browser's local storage and will persist across sessions on this device.