Skip to content

Commit f874906

Browse files
authored
Merge pull request #452 from sir-gon/feature/array_left_rotation
[Hacker Rank] Interview Preparation Kit: Arrays: Left Rotation. Solve…
2 parents 6405abb + 19d5f2a commit f874906

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed
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]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.md]]
3+
*/
4+
5+
export function rotLeftOne(aNumbers) {
6+
const first = aNumbers.shift();
7+
if (first !== undefined) {
8+
aNumbers.push(first);
9+
}
10+
11+
return aNumbers;
12+
}
13+
14+
export function rotLeft(aNumbers, dRotations) {
15+
let output = [...aNumbers];
16+
17+
for (let i = 0; i < dRotations; i++) {
18+
output = rotLeftOne(output);
19+
}
20+
21+
return output;
22+
}
23+
24+
export default { rotLeft, rotLeftOne };
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { describe, expect, it } from '@jest/globals';
2+
import { logger as console } from '../../../logger.js';
3+
4+
import { rotLeft, rotLeftOne } from './ctci_array_left_rotation.js';
5+
6+
const ROT_LEFT_ONE_TEST_CASES = [
7+
{ numbers: [1, 2, 3, 4, 5], expected: [2, 3, 4, 5, 1] },
8+
{ numbers: [2, 3, 4, 5, 1], expected: [3, 4, 5, 1, 2] },
9+
{ numbers: [3, 4, 5, 1, 2], expected: [4, 5, 1, 2, 3] },
10+
{ numbers: [4, 5, 1, 2, 3], expected: [5, 1, 2, 3, 4] },
11+
{ numbers: [5, 1, 2, 3, 4], expected: [1, 2, 3, 4, 5] }
12+
];
13+
14+
const ROT_LEFT_TEST_CASES = [
15+
{ numbers: [1, 2, 3, 4, 5], d_rotations: 4, expected: [5, 1, 2, 3, 4] }
16+
];
17+
18+
describe('ctci_array_left_rotation', () => {
19+
it('rotLeftOne Test Cases', () => {
20+
expect.assertions(5);
21+
22+
ROT_LEFT_ONE_TEST_CASES.forEach((value) => {
23+
const answer = rotLeftOne(value.numbers);
24+
25+
console.debug(`rotLeftOne(${value.numbers}) solution found: ${answer}`);
26+
27+
expect(answer).toStrictEqual(value.expected);
28+
});
29+
});
30+
31+
it('rotLeft Test cases', () => {
32+
expect.assertions(1);
33+
34+
ROT_LEFT_TEST_CASES.forEach((value) => {
35+
const answer = rotLeft(value.numbers, value.d_rotations);
36+
37+
console.debug(`rotLeft(${value.numbers}) solution found: ${answer}`);
38+
39+
expect(answer).toStrictEqual(value.expected);
40+
});
41+
});
42+
});

0 commit comments

Comments
 (0)