-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeftRotationIterative.java
More file actions
46 lines (39 loc) · 1.03 KB
/
Copy pathLeftRotationIterative.java
File metadata and controls
46 lines (39 loc) · 1.03 KB
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
41
42
43
44
45
46
/*
Input format
5 4 // number of elements, number of times to rotate
1 2 3 4 5 // elements in the array
*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class LeftRotationIterative {
public static int[] arrayLeftRotation(int[] a, int n, int k) {
int size = a.length;
int x = 0;
while(x < k){
int tmp = a[0];
for(int i=1;i<size;i++){
a[i-1] = a[i];
}
a[size-1] = tmp;
x++;
}
return a;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int k = in.nextInt();
int a[] = new int[n];
for(int a_i=0; a_i < n; a_i++){
a[a_i] = in.nextInt();
}
int[] output = new int[n];
output = arrayLeftRotation(a, n, k);
for(int i = 0; i < n; i++)
System.out.print(output[i] + " ");
System.out.println();
}
}