Skip to content

Commit 17b82b7

Browse files
committed
Added array left rotation solution in ds category
1 parent 20c6a2c commit 17b82b7

File tree

1 file changed

+46
-0
lines changed
  • HackerRankDashboard/CoreCS/DataStructures/src/main/java/com/javaaid/hackerrank/solutions/datastructures/arrays

1 file changed

+46
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
*
3+
* Problem Statement-
4+
* [Arrays: Left Rotation](https://www.hackerrank.com/challenges/ctci-array-left-rotation/problem)
5+
* [Tutorial](https://youtu.be/TurKRD6Ne6w)
6+
*/
7+
package com.javaaid.hackerrank.solutions.datastructures.arrays;
8+
9+
import java.util.Arrays;
10+
import java.util.Scanner;
11+
12+
/**
13+
* @author Kanahaiya Gupta
14+
*
15+
*/
16+
public class LeftRotation {
17+
18+
static int[] rotLeft(int[] a, int d) {
19+
int n = a.length;
20+
int[] rotArray = new int[n];
21+
22+
for (int oldIndex = 0; oldIndex < n; oldIndex++) {
23+
int newIndex = (oldIndex + n - d) % n;
24+
rotArray[newIndex] = a[oldIndex];
25+
}
26+
27+
return rotArray;
28+
29+
}
30+
31+
public static void main(String[] args) {
32+
33+
Scanner sc = new Scanner(System.in);
34+
int arraySize = sc.nextInt();
35+
int d = sc.nextInt();
36+
int a[] = new int[arraySize];
37+
for (int i = 0; i < a.length; i++) {
38+
a[i] = sc.nextInt();
39+
}
40+
41+
int res[] = rotLeft(a, d);
42+
System.out.println(Arrays.toString(res));
43+
sc.close();
44+
}
45+
46+
}

0 commit comments

Comments
 (0)