Skip to content

Commit 66226c9

Browse files
authored
Merge pull request #62 from sir-gon/feature/ctci-array-left-rotation
[Hacker Rank] Interview Preparation Kit: Arrays: Left Rotation. Solve…
2 parents b698cbf + 59cd65e commit 66226c9

File tree

3 files changed

+162
-0
lines changed

3 files changed

+162
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit;
2+
3+
[TestClass]
4+
public class ArraysLeftRotationTest
5+
{
6+
public class ArraysLeftRotationTestCase
7+
{
8+
public List<int> input = []; public List<int> expected = [];
9+
}
10+
11+
public class ArraysLeftRotationsTestCase
12+
{
13+
public List<int> input = []; public int d; public List<int> expected = [];
14+
}
15+
16+
private static readonly ArraysLeftRotationTestCase[] tests = [
17+
new() { input = [1, 2, 3, 4, 5], expected = [2, 3, 4, 5, 1] },
18+
new() { input = [2, 3, 4, 5, 1], expected = [3, 4, 5, 1, 2] },
19+
new() { input = [3, 4, 5, 1, 2], expected = [4, 5, 1, 2, 3] },
20+
new() { input = [4, 5, 1, 2, 3], expected = [5, 1, 2, 3, 4] },
21+
new() { input = [5, 1, 2, 3, 4], expected = [1, 2, 3, 4, 5] }
22+
];
23+
24+
private static readonly ArraysLeftRotationsTestCase[] testRotationsCases = [
25+
new() { input = [1, 2, 3, 4, 5], d = 4, expected = [5, 1, 2, 3, 4] }
26+
];
27+
28+
[TestMethod]
29+
public void testRotLeftOne()
30+
{
31+
List<int> result;
32+
33+
foreach (ArraysLeftRotationTestCase test in tests)
34+
{
35+
result = ArraysLeftRotation.rotLeftOne(test.input);
36+
CollectionAssert.AreEquivalent(test.expected, result);
37+
}
38+
}
39+
40+
41+
[TestMethod]
42+
public void testRotLeft()
43+
{
44+
List<int> result;
45+
46+
foreach (ArraysLeftRotationsTestCase test in testRotationsCases)
47+
{
48+
result = ArraysLeftRotation.rotLeft(test.input, test.d);
49+
CollectionAssert.AreEquivalent(test.expected, result);
50+
}
51+
}
52+
}
53+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// @link Problem definition [[docs/hackerrank/projecteuler/euler001.md]]
2+
3+
namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit;
4+
5+
using System.Diagnostics.CodeAnalysis;
6+
7+
public class ArraysLeftRotation
8+
{
9+
[ExcludeFromCodeCoverage]
10+
protected ArraysLeftRotation() { }
11+
12+
public const int FIRST_POSITION = 0;
13+
14+
/**
15+
* In favor of increasing performance, this implementation mutates the input list.
16+
*/
17+
public static List<int> rotLeftOne(List<int> input)
18+
{
19+
int first = input[FIRST_POSITION];
20+
input.RemoveAt(FIRST_POSITION);
21+
input.Add(first);
22+
23+
return input;
24+
}
25+
26+
27+
/**
28+
* This implementation does not mutate the input list.
29+
*/
30+
public static List<int> rotLeft(List<int> input, int d)
31+
{
32+
// Clone the list
33+
List<int> output = input.GetRange(FIRST_POSITION, input.Count);
34+
35+
int i = 1;
36+
while (i <= d)
37+
{
38+
output = rotLeftOne(output);
39+
i += 1;
40+
}
41+
42+
return output;
43+
}
44+
}
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)