From 59fbe785117be98a90ccbfde2f7520e73c6da4d9 Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Tue, 2 Jul 2024 17:48:44 -0400 Subject: [PATCH] =?UTF-8?q?[Hacker=20Rank]=20Interview=20Preparation=20Kit?= =?UTF-8?q?:=20Arrays:=20Left=20Rotation.=20Solved=20=E2=9C=85.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../arrays/ctci_array_left_rotation.md | 65 +++++++++++++++++++ .../arrays/ctci_array_left_rotation.test.ts | 42 ++++++++++++ .../arrays/ctci_array_left_rotation.ts | 24 +++++++ 3 files changed, 131 insertions(+) create mode 100644 docs/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.md create mode 100644 src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.test.ts create mode 100644 src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.ts diff --git a/docs/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.md b/docs/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.md new file mode 100644 index 00000000..7b4e341b --- /dev/null +++ b/docs/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.md @@ -0,0 +1,65 @@ +# [Arrays: Left Rotation](https://www.hackerrank.com/challenges/ctci-array-left-rotation) + +Given an array and a number, d, perform d left rotations on the array. + +- Difficulty: #easy +- Category: #ProblemSolvingBasic + +A left rotation operation on an array shifts each of the array's elements +$ 1 $ unit to the left. For example, if $ 2 $ left rotations are performed +on array $ [1, 2, 3, 4, 5] $, then the array would become $ [3, 4, 5, 1, 2] $. +Note that the lowest index item moves to the highest index in a rotation. +This is called a circular array. + +Given an array $ a $ of $ n $ integers and a number, $ d $, perform $ d $ left +rotations on the array. Return the updated array to be printed as a single +line of space-separated integers. + +## Function Description + +Complete the function rotLeft in the editor below. + +rotLeft has the following parameter(s): + +- int a[n]: the array to rotate +- int d: the number of rotations + +## Returns + +- int a'[n]: the rotated array + +## Input Format + +The first line contains two space-separated integers $ n $ and $ d $, the size +of $ a $ and the number of left rotations. +The second line contains $ n $ space-separated integers, each an $ a[i] $. + +## Constraints + +- $ 1 \leq n \leq 10^5 $ +- $ 1 \leq d \leq n $ +- $ 1 \leq a[i] \leq 10^6 $ + +## Sample Input + +```text +5 4 +1 2 3 4 5 +``` + +## Sample Output + +```text +5 1 2 3 4 +``` + +## Explanation + +When we perform $ d = 4 $ left rotations, the array undergoes the following +sequence of changes: + +> [1, 2, 3, 4, 5] +> -> [2, 3, 4, 5, 1] +> -> [3, 4, 5, 1, 2] +> -> [4, 5, 1, 2, 3] +> -> [5, 1, 2, 3, 4] diff --git a/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.test.ts b/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.test.ts new file mode 100644 index 00000000..acf53e9c --- /dev/null +++ b/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.test.ts @@ -0,0 +1,42 @@ +import { describe, expect, it } from '@jest/globals'; +import { logger as console } from '../../../logger'; + +import { rotLeft, rotLeftOne } from './ctci_array_left_rotation'; + +const ROT_LEFT_ONE_TEST_CASES = [ + { numbers: [1, 2, 3, 4, 5], expected: [2, 3, 4, 5, 1] }, + { numbers: [2, 3, 4, 5, 1], expected: [3, 4, 5, 1, 2] }, + { numbers: [3, 4, 5, 1, 2], expected: [4, 5, 1, 2, 3] }, + { numbers: [4, 5, 1, 2, 3], expected: [5, 1, 2, 3, 4] }, + { numbers: [5, 1, 2, 3, 4], expected: [1, 2, 3, 4, 5] } +]; + +const ROT_LEFT_TEST_CASES = [ + { numbers: [1, 2, 3, 4, 5], d_rotations: 4, expected: [5, 1, 2, 3, 4] } +]; + +describe('ctci_array_left_rotation', () => { + it('rotLeftOne Test Cases', () => { + expect.assertions(5); + + ROT_LEFT_ONE_TEST_CASES.forEach((value) => { + const answer = rotLeftOne(value.numbers); + + console.debug(`rotLeftOne(${value.numbers}) solution found: ${answer}`); + + expect(answer).toStrictEqual(value.expected); + }); + }); + + it('rotLeft Test cases', () => { + expect.assertions(1); + + ROT_LEFT_TEST_CASES.forEach((value) => { + const answer = rotLeft(value.numbers, value.d_rotations); + + console.debug(`rotLeft(${value.numbers}) solution found: ${answer}`); + + expect(answer).toStrictEqual(value.expected); + }); + }); +}); diff --git a/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.ts b/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.ts new file mode 100644 index 00000000..5befbdd5 --- /dev/null +++ b/src/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.ts @@ -0,0 +1,24 @@ +/** + * @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.md]] + */ + +export function rotLeftOne(a_numbers: number[]): number[] { + const first = a_numbers.shift(); + if (first != undefined) { + a_numbers.push(first); + } + + return a_numbers; +} + +export function rotLeft(a_numbers: number[], d_rotations: number): number[] { + let output = [...a_numbers]; + + for (let i = 0; i < d_rotations; i++) { + output = rotLeftOne(output); + } + + return output; +} + +export default { rotLeft, rotLeftOne };