-
Notifications
You must be signed in to change notification settings - Fork 858
/
LeftRotation.java
46 lines (36 loc) · 982 Bytes
/
LeftRotation.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
*
* Problem Statement-
* [Arrays: Left Rotation](https://www.hackerrank.com/challenges/ctci-array-left-rotation/problem)
* [Tutorial](https://youtu.be/TurKRD6Ne6w)
*/
package com.javaaid.hackerrank.solutions.tutorials.ctci;
import java.util.Arrays;
import java.util.Scanner;
/**
* @author Kanahaiya Gupta
*
*/
public class LeftRotation {
static int[] rotLeft(int[] a, int d) {
int n = a.length;
int[] rotArray = new int[n];
for (int oldIndex = 0; oldIndex < n; oldIndex++) {
int newIndex = (oldIndex + n - d) % n;
rotArray[newIndex] = a[oldIndex];
}
return rotArray;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int arraySize = sc.nextInt();
int d = sc.nextInt();
int a[] = new int[arraySize];
for (int i = 0; i < a.length; i++) {
a[i] = sc.nextInt();
}
int res[] = rotLeft(a, d);
System.out.println(Arrays.toString(res));
sc.close();
}
}