-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathLeftRotation.java
More file actions
55 lines (45 loc) · 1.5 KB
/
Copy pathLeftRotation.java
File metadata and controls
55 lines (45 loc) · 1.5 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
47
48
49
50
51
52
53
54
55
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package hackerrank;
import java.util.Arrays;
import java.util.Scanner;
/**
*
* @author Jayit
*/
public class LeftRotation {
// Here it is required to left rotate the array by one place,
//but it is noticed that right rotation is more feasible
//By analyzing 1 left rotation means total length of array - no of right rotations
//That is one right rotation is n - d
//now we right rotate by that amt
//make a rotArray same length as passed array
//we cal the newIndex as (oldIndex + n - d) % n;
// % modulo so that it doesnt exceed thae array length at all
// Now newIndex in the rotArray would contain value of the oldIndex value of parent array
// Finally return rotArray
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();
}
}