-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRotateByK.java
More file actions
35 lines (30 loc) · 812 Bytes
/
RotateByK.java
File metadata and controls
35 lines (30 loc) · 812 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package Array;
public class RotateByK {
/**
* @param a - It is the array under consideration.
* @param k - It is the steps which the array must be rotated.
*/
public static void rotate(int[] a, int k) {
// In-Case : If k > a.length
k %= a.length;
reverse(a, 0, a.length - 1);
reverse(a, 0, k - 1);
reverse(a, k, a.length - 1);
}
private static void reverse(int[] a, int i, int j) {
int temp;
while (i < j) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
i++;
j--;
}
}
// Time Complexity - O(n), Auxiliary Space - O(1)
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
int k = 2;
rotate(arr, k);
}
}