Input: nums =[1,2,3,4,5,6,7], k =3Output: [4,5,6,7,1,2,3]Explanation:
rotate 1 steps to the right:[2,3,4,5,6,7,1]rotate 2 steps to the right:[3,4,5,6,7,1,2]rotate 3 steps to the right:[4,5,6,7,1,2,3]
To rotate an array left by k positions, we need to shift the array to the left by k % n, making space on the right for the first k % n elements of the original array. The simple solution involves using a temporary array of size k % n to store the leftmost k % n elements. Then, shift the original array to the left by k % n positions, and finally copy the saved elements from the temporary array to the right side of the original array.
classSolution {
publicvoidrotateLeft(int[] nums, int k) {
int n = nums.length;
k = k % n; // Ensure k is within the bounds of the array sizeint[] result =newint[nums.length];
for (int i = 0; i < n - k; i++) {
result[i]= nums[k + i];
}
for (int i = 0; i < k; i++) {
result[n - k + i]= nums[i];
}
System.arraycopy(result, 0, nums, 0, nums.length);
}
publicstaticvoidmain(String[] args) {
Solution sol =new Solution();
int[] arr = {1, 2, 3, 4, 5};
int k = 2;
sol.rotateLeft(arr, k);
for (int num : arr) {
System.out.print(num +" ");
}
// Expected output: [3, 4, 5, 1, 2] }
}
classSolution:
defrotateLeft(self, nums: List[int], k: int) ->None:
n = len(nums)
k = k % n # Ensure k is within the bounds of the array size# Create a temporary array to store the result result = [0] * n
# Copy the elements after the first k elements to the beginning of the result arrayfor i in range(n - k):
result[i] = nums[k + i]
# Copy the first k elements to the end of the result arrayfor i in range(k):
result[n - k + i] = nums[i]
# Copy the result array back into the original arrayfor i in range(n):
nums[i] = result[i]
# Example usagesol = Solution()
arr = [1, 2, 3, 4, 5]
k =2sol.rotateLeft(arr, k)
print(arr) # Expected output: [3, 4, 5, 1, 2]
classSolution {
publicvoidrotateLeft(int[] arr, int k) {
if (arr ==null|| k < 0) {
thrownew IllegalArgumentException("Illegal argument!");
}
int n = arr.length;
k = k % n; // Ensure k is within the bounds of the array sizefor (int i = 0; i < k; i++) {
for (int j = 0; j < n - 1; j++) {
swap(arr, j, j + 1);
}
}
}
voidswap(int[] arr, int i, int j) {
int tmp = arr[i];
arr[i]= arr[j];
arr[j]= tmp;
}
publicstaticvoidmain(String[] args) {
Solution sol =new Solution();
int[] arr = {1, 2, 3, 4, 5};
int k = 2;
sol.rotateLeft(arr, k);
for (int num : arr) {
System.out.print(num +" ");
}
// Expected output: [3, 4, 5, 1, 2] }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution:
defrotateLeft(self, arr: list[int], k: int) ->None:
if arr isNoneor k <0:
raiseValueError("Illegal argument!")
n = len(arr)
k = k % n # Ensure k is within the bounds of the array sizefor i in range(k):
for j in range(n -1):
self.swap(arr, j, j +1)
defswap(self, arr: list[int], i: int, j: int) ->None:
arr[i], arr[j] = arr[j], arr[i]
To perform a left rotation on an array, we can use the reverse operation which involves three main steps. This method is efficient as it reverses segments of the array in place.
classSolution {
publicvoidrotateArrayLeft(int[] arr, int k) {
int n = arr.length;
k = k % n; // Ensure k is within bounds reverse(arr, 0, k - 1); // Reverse the first part reverse(arr, k, n - 1); // Reverse the second part reverse(arr, 0, n - 1); // Reverse the whole array }
publicvoidreverse(int[] arr, int start, int end) {
while (start < end) {
swap(arr, start, end);
start++;
end--;
}
}
publicvoidswap(int[] arr, int i, int j) {
int tmp = arr[i];
arr[i]= arr[j];
arr[j]= tmp;
}
publicstaticvoidmain(String[] args) {
Solution sol =new Solution();
int[] arr = {1, 2, 3, 4, 5, 6};
int k = 2;
sol.rotateArrayLeft(arr, k);
for (int num : arr) {
System.out.print(num +" ");
}
// Expected output: [3, 4, 5, 6, 1, 2] }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
classSolution:
defrotate_left(self, arr: list[int], k: int) ->None:
n = len(arr)
k = k % n # Ensure k is within bounds self.reverse(arr, 0, k -1) # Reverse the first part self.reverse(arr, k, n -1) # Reverse the second part self.reverse(arr, 0, n -1) # Reverse the whole arraydefreverse(self, arr: list[int], start: int, end: int) ->None:
while start < end:
self.swap(arr, start, end)
start +=1 end -=1defswap(self, arr: list[int], i: int, j: int) ->None:
arr[i], arr[j] = arr[j], arr[i]