-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathSolution.java
31 lines (25 loc) · 1 KB
/
Solution.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
package Practice.CrackingTheCodingInterview.ArraysLeftRotation;
import java.util.Scanner;
public class Solution {
private static int[] leftRotation(int[] a, int d) {
int shiftsAfterCompleteRotations = d % a.length;
int[] resultList = new int[a.length];
System.arraycopy(a, shiftsAfterCompleteRotations, resultList, 0, a.length - shiftsAfterCompleteRotations);
System.arraycopy(a, 0, resultList, a.length - shiftsAfterCompleteRotations, shiftsAfterCompleteRotations);
return resultList;
}
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[] result = leftRotation(a, k);
for (int i = 0; i < result.length; i++) {
System.out.print(result[i]);
if (i != result.length - 1) System.out.print(" ");
}
}
}