From 59cd65ecfebb4de51bfe8b3e1b7ab6e46aa6d70c Mon Sep 17 00:00:00 2001 From: Gonzalo Diaz Date: Thu, 6 Jun 2024 16:15:13 -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/ArraysLeftRotation.Test.cs | 53 +++++++++++++++ .../arrays/ArraysLeftRotation.cs | 44 +++++++++++++ .../arrays/ctci_array_left_rotation.md | 65 +++++++++++++++++++ 3 files changed, 162 insertions(+) create mode 100644 algorithm-exercises-csharp-test/src/hackerrank/interview_preparation_kit/arrays/ArraysLeftRotation.Test.cs create mode 100644 algorithm-exercises-csharp/src/hackerrank/interview_preparation_kit/arrays/ArraysLeftRotation.cs create mode 100644 docs/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.md diff --git a/algorithm-exercises-csharp-test/src/hackerrank/interview_preparation_kit/arrays/ArraysLeftRotation.Test.cs b/algorithm-exercises-csharp-test/src/hackerrank/interview_preparation_kit/arrays/ArraysLeftRotation.Test.cs new file mode 100644 index 0000000..f5d1c73 --- /dev/null +++ b/algorithm-exercises-csharp-test/src/hackerrank/interview_preparation_kit/arrays/ArraysLeftRotation.Test.cs @@ -0,0 +1,53 @@ +namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit; + +[TestClass] +public class ArraysLeftRotationTest +{ + public class ArraysLeftRotationTestCase + { + public List input = []; public List expected = []; + } + + public class ArraysLeftRotationsTestCase + { + public List input = []; public int d; public List expected = []; + } + + private static readonly ArraysLeftRotationTestCase[] tests = [ + new() { input = [1, 2, 3, 4, 5], expected = [2, 3, 4, 5, 1] }, + new() { input = [2, 3, 4, 5, 1], expected = [3, 4, 5, 1, 2] }, + new() { input = [3, 4, 5, 1, 2], expected = [4, 5, 1, 2, 3] }, + new() { input = [4, 5, 1, 2, 3], expected = [5, 1, 2, 3, 4] }, + new() { input = [5, 1, 2, 3, 4], expected = [1, 2, 3, 4, 5] } + ]; + + private static readonly ArraysLeftRotationsTestCase[] testRotationsCases = [ + new() { input = [1, 2, 3, 4, 5], d = 4, expected = [5, 1, 2, 3, 4] } + ]; + + [TestMethod] + public void testRotLeftOne() + { + List result; + + foreach (ArraysLeftRotationTestCase test in tests) + { + result = ArraysLeftRotation.rotLeftOne(test.input); + CollectionAssert.AreEquivalent(test.expected, result); + } + } + + + [TestMethod] + public void testRotLeft() + { + List result; + + foreach (ArraysLeftRotationsTestCase test in testRotationsCases) + { + result = ArraysLeftRotation.rotLeft(test.input, test.d); + CollectionAssert.AreEquivalent(test.expected, result); + } + } +} + diff --git a/algorithm-exercises-csharp/src/hackerrank/interview_preparation_kit/arrays/ArraysLeftRotation.cs b/algorithm-exercises-csharp/src/hackerrank/interview_preparation_kit/arrays/ArraysLeftRotation.cs new file mode 100644 index 0000000..70d402a --- /dev/null +++ b/algorithm-exercises-csharp/src/hackerrank/interview_preparation_kit/arrays/ArraysLeftRotation.cs @@ -0,0 +1,44 @@ +// @link Problem definition [[docs/hackerrank/projecteuler/euler001.md]] + +namespace algorithm_exercises_csharp.hackerrank.interview_preparation_kit; + +using System.Diagnostics.CodeAnalysis; + +public class ArraysLeftRotation +{ + [ExcludeFromCodeCoverage] + protected ArraysLeftRotation() { } + + public const int FIRST_POSITION = 0; + + /** + * In favor of increasing performance, this implementation mutates the input list. + */ + public static List rotLeftOne(List input) + { + int first = input[FIRST_POSITION]; + input.RemoveAt(FIRST_POSITION); + input.Add(first); + + return input; + } + + + /** + * This implementation does not mutate the input list. + */ + public static List rotLeft(List input, int d) + { + // Clone the list + List output = input.GetRange(FIRST_POSITION, input.Count); + + int i = 1; + while (i <= d) + { + output = rotLeftOne(output); + i += 1; + } + + return output; + } +} 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 0000000..7b4e341 --- /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]