-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRotate array
36 lines (31 loc) · 860 Bytes
/
Rotate array
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
36
import java.io.*;
import java.util.*;
public class Main {
public static void main(String [] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arr[] = new int[n];
for(int i = 0 ; i < arr.length ; i++){
arr[i] = sc.nextInt();
}
int k = sc.nextInt();
k = k % arr.length;
reverse(arr,0,arr.length-k-1);
reverse(arr,arr.length-k,arr.length-1);
reverse(arr,0,arr.length-1);
for(int i = 0 ; i < arr.length; i++){
System.out.print(arr[i]+" ");
}
}
public static void reverse(int[] arr,int i, int j){
int li = i;
int ri = j;
while(li < ri){
int temp = arr[li];
arr[li] = arr[ri];
arr[ri] = temp;
li++;
ri--;
}
}
}