-
Notifications
You must be signed in to change notification settings - Fork 331
/
Main.java
32 lines (32 loc) · 886 Bytes
/
Main.java
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
import java.util.*;
public class Main {
// Function to Reverse the array
public static void Reverse(int[] arr, int start, int end) {
while (start <= end) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
// Function to Rotate k elements to right
public static void Rotateeletoright(int[] arr, int n, int k) {
// Reverse first n-k elements
Reverse(arr, 0, n - k - 1);
// Reverse last k elements
Reverse(arr, n - k, n - 1);
// Reverse whole array
Reverse(arr, 0, n - 1);
}
public static void main(String args[]) {
int[] arr = {1,2,3,4,5,6,7};
int n = 7;
int k = 2;
Rotateeletoright(arr, n, k);
System.out.print("After Rotating the k elements to right ");
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
System.out.println();
}
}