Skip to content

Commit 106b573

Browse files
authored
Merge pull request #178 from sir-gon/feature/array-left-rotation
Feature/array left rotation
2 parents 5bc28b7 + 98f2e81 commit 106b573

File tree

4 files changed

+203
-2
lines changed

4 files changed

+203
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package ae.hackerrank.interview_preparation_kit.arrays;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
7+
/**
8+
* Arrays Left Rotation.
9+
*
10+
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.md]]
11+
*/
12+
public class ArraysLeftRotation {
13+
14+
private ArraysLeftRotation() {
15+
}
16+
17+
static java.util.logging.Logger logger = util.CustomLogger.getLogger();
18+
static final int FIRST_POSITION = 0;
19+
20+
/**
21+
* rotLeftOne.
22+
*/
23+
public static List<Integer> rotLeftOne(List<Integer> a) {
24+
// Clone the list
25+
List<Integer> output = new ArrayList<>(a);
26+
27+
Integer first = output.get(FIRST_POSITION);
28+
output.remove(FIRST_POSITION);
29+
output.add(first);
30+
31+
return output;
32+
}
33+
34+
/**
35+
* rotLeft.
36+
*/
37+
public static List<Integer> rotLeft(List<Integer> a, int d) {
38+
39+
// Clone the list
40+
List<Integer> output = new ArrayList<>(a);
41+
42+
int i = 1;
43+
while (i <= d) {
44+
output = rotLeftOne(output);
45+
i += 1;
46+
}
47+
48+
return output;
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package ae.hackerrank.interview_preparation_kit.arrays;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.Arrays;
6+
import java.util.List;
7+
import org.junit.jupiter.api.BeforeAll;
8+
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.api.TestInstance;
10+
import org.junit.jupiter.api.TestInstance.Lifecycle;
11+
12+
13+
@TestInstance(Lifecycle.PER_CLASS)
14+
class ArraysLeftRotationTest {
15+
16+
public class ArraysLeftRotationTestCase {
17+
public List<Integer> input;
18+
public List<Integer> expected;
19+
20+
public ArraysLeftRotationTestCase(List<Integer> input, List<Integer> expected) {
21+
this.input = input;
22+
this.expected = expected;
23+
}
24+
}
25+
26+
public class ArraysLeftRotationsTestCase {
27+
public List<Integer> input;
28+
public Integer d;
29+
public List<Integer> expected;
30+
31+
public ArraysLeftRotationsTestCase(List<Integer> input, Integer d, List<Integer> expected) {
32+
this.input = input;
33+
this.d = d;
34+
this.expected = expected;
35+
}
36+
}
37+
38+
public List<ArraysLeftRotationTestCase> testCases;
39+
public List<ArraysLeftRotationsTestCase> testRotationsCases;
40+
41+
@BeforeAll
42+
public void setup() {
43+
this.testCases = Arrays.asList(
44+
new ArraysLeftRotationTestCase(Arrays.asList(1, 2, 3, 4, 5), Arrays.asList(2, 3, 4, 5, 1)),
45+
new ArraysLeftRotationTestCase(Arrays.asList(2, 3, 4, 5, 1), Arrays.asList(3, 4, 5, 1, 2)),
46+
new ArraysLeftRotationTestCase(Arrays.asList(3, 4, 5, 1, 2), Arrays.asList(4, 5, 1, 2, 3)),
47+
new ArraysLeftRotationTestCase(Arrays.asList(4, 5, 1, 2, 3), Arrays.asList(5, 1, 2, 3, 4)),
48+
new ArraysLeftRotationTestCase(Arrays.asList(5, 1, 2, 3, 4), Arrays.asList(1, 2, 3, 4, 5))
49+
);
50+
51+
this.testRotationsCases = Arrays.asList(
52+
new ArraysLeftRotationsTestCase(
53+
Arrays.asList(1, 2, 3, 4, 5),
54+
4,
55+
Arrays.asList(5, 1, 2, 3, 4))
56+
);
57+
}
58+
59+
@Test void testRotLeftOne() {
60+
for (ArraysLeftRotationTestCase testCase : this.testCases) {
61+
List<Integer> solutionFound = ArraysLeftRotation.rotLeftOne(testCase.input);
62+
63+
assertEquals(testCase.expected, solutionFound,
64+
String.format("%s(%s) answer must be: %s",
65+
"CompareTriplets.compareTriplets",
66+
testCase.input.toString(),
67+
testCase.expected.toString())
68+
);
69+
}
70+
}
71+
72+
@Test void testRotLeft() {
73+
for (ArraysLeftRotationsTestCase testCase : this.testRotationsCases) {
74+
List<Integer> solutionFound = ArraysLeftRotation.rotLeft(testCase.input, testCase.d);
75+
76+
assertEquals(testCase.expected, solutionFound,
77+
String.format("%s(%s, %d) answer must be: %s",
78+
"CompareTriplets.compareTriplets",
79+
testCase.input.toString(),
80+
testCase.d,
81+
testCase.expected.toString())
82+
);
83+
}
84+
}
85+
86+
}

config/checkstyle/checkstyle.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
<module name="LineLength">
4646
<property name="fileExtensions" value="java"/>
4747
<property name="max" value="100"/>
48-
<property name="ignorePattern" value="^package.*|^import.*|a href|href|http://|https://|ftp://"/>
48+
<property name="ignorePattern" value="^package.*|^import.*|a href|href|http://|https://|ftp://|[[*]]"/>
4949
</module>
5050

5151
<module name="TreeWalker">
@@ -185,7 +185,7 @@
185185
value="Type name ''{0}'' must match pattern ''{1}''."/>
186186
</module>
187187
<module name="MemberName">
188-
<property name="format" value="^[_]*[a-z][a-z0-9][a-zA-Z0-9]*$"/>
188+
<property name="format" value="^[_]*[a-z][a-zA-Z0-9]*$"/>
189189
<message key="name.invalidPattern"
190190
value="Member name ''{0}'' must match pattern ''{1}''."/>
191191
</module>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# [Arrays: Left Rotation](https://www.hackerrank.com/challenges/ctci-array-left-rotation)
2+
3+
Given an array and a number, d, perform d left rotations on the array.
4+
5+
- Difficulty: #easy
6+
- Category: #ProblemSolvingBasic
7+
8+
A left rotation operation on an array shifts each of the array's elements
9+
$ 1 $ unit to the left. For example, if $ 2 $ left rotations are performed
10+
on array $ [1, 2, 3, 4, 5] $, then the array would become $ [3, 4, 5, 1, 2] $.
11+
Note that the lowest index item moves to the highest index in a rotation.
12+
This is called a circular array.
13+
14+
Given an array $ a $ of $ n $ integers and a number, $ d $, perform $ d $ left
15+
rotations on the array. Return the updated array to be printed as a single
16+
line of space-separated integers.
17+
18+
## Function Description
19+
20+
Complete the function rotLeft in the editor below.
21+
22+
rotLeft has the following parameter(s):
23+
24+
- int a[n]: the array to rotate
25+
- int d: the number of rotations
26+
27+
## Returns
28+
29+
- int a'[n]: the rotated array
30+
31+
## Input Format
32+
33+
The first line contains two space-separated integers $ n $ and $ d $, the size
34+
of $ a $ and the number of left rotations.
35+
The second line contains $ n $ space-separated integers, each an $ a[i] $.
36+
37+
## Constraints
38+
39+
- $ 1 \leq n \leq 10^5 $
40+
- $ 1 \leq d \leq n $
41+
- $ 1 \leq a[i] \leq 10^6 $
42+
43+
## Sample Input
44+
45+
```text
46+
5 4
47+
1 2 3 4 5
48+
```
49+
50+
## Sample Output
51+
52+
```text
53+
5 1 2 3 4
54+
```
55+
56+
## Explanation
57+
58+
When we perform $ d = 4 $ left rotations, the array undergoes the following
59+
sequence of changes:
60+
61+
> [1, 2, 3, 4, 5]
62+
> -> [2, 3, 4, 5, 1]
63+
> -> [3, 4, 5, 1, 2]
64+
> -> [4, 5, 1, 2, 3]
65+
> -> [5, 1, 2, 3, 4]

0 commit comments

Comments
 (0)