-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeft_rotate_array_D_places.java
More file actions
40 lines (36 loc) · 972 Bytes
/
Left_rotate_array_D_places.java
File metadata and controls
40 lines (36 loc) · 972 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
36
37
38
39
40
import java.util.Scanner;
public class Main
{
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--;
}
return;
}
static void Left_rotate_array_D_places(int []arr,int len,int d){
d=d%len;
if(len<=1 || d==0){
return;
}
reverse(arr,0,d-1);
reverse(arr,d,len-1);
reverse(arr,0,len-1);
for(int i = 0; i < len; i++){
System.out.print(arr[i]+" ");
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int len = input.nextInt();
int[] arr = new int[len];
for (int i = 0; i < len; i++) {
arr[i] = input.nextInt();
}
int d=input.nextInt();
Left_rotate_array_D_places(arr,len,d);
}
}