-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeftRotation.java
More file actions
64 lines (43 loc) · 1.38 KB
/
Copy pathLeftRotation.java
File metadata and controls
64 lines (43 loc) · 1.38 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
56
57
58
59
60
61
62
63
64
package com.HackerRank;
import java.util.Scanner;
public class LeftRotation {
/*
* Hacker Rank Algorithm Question - Left Rotation - Solution
* *
* Category : Data Structure
* Solved Date : 2019-08-21
* Author : TK Lee
*
* Source : https://www.hackerrank.com/challenges/array-left-rotation/problem
*/
public static int[] toTheLeft (int[] arr, int shift){
int [] shifted = new int [arr.length];
int cur = 0 ;
for (int i = arr.length -1 ; i >= 0; i--){
cur = i - shift ;
if(cur < 0) {
cur = arr.length - Math.abs(cur);
}
shifted[cur] = arr[i] ;
}
return shifted;
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
String[] nd = scanner.nextLine().split(" ");
int n = Integer.parseInt(nd[0]);
int d = Integer.parseInt(nd[1]);
int[] a = new int[n];
String[] aItems = scanner.nextLine().split(" ");
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
for (int i = 0; i < n; i++) {
int aItem = Integer.parseInt(aItems[i]);
a[i] = aItem;
}
a = toTheLeft(a, d);
for (int i = 0; i<a.length; i++){
System.out.print(a[i] + " ");
}
scanner.close();
}
}