diff --git a/algorithm-exercises-java/src/main/java/ae/hackerrank/interview_preparation_kit/arrays/ArraysLeftRotation.java b/algorithm-exercises-java/src/main/java/ae/hackerrank/interview_preparation_kit/arrays/ArraysLeftRotation.java new file mode 100644 index 0000000..868bf97 --- /dev/null +++ b/algorithm-exercises-java/src/main/java/ae/hackerrank/interview_preparation_kit/arrays/ArraysLeftRotation.java @@ -0,0 +1,50 @@ +package ae.hackerrank.interview_preparation_kit.arrays; + +import java.util.ArrayList; +import java.util.List; + + +/** + * Arrays Left Rotation. + * + * @link Problem definition [[docs/hackerrank/interview_preparation_kit/arrays/ctci_array_left_rotation.md]] + */ +public class ArraysLeftRotation { + + private ArraysLeftRotation() { + } + + static java.util.logging.Logger logger = util.CustomLogger.getLogger(); + static final int FIRST_POSITION = 0; + + /** + * rotLeftOne. + */ + public static List rotLeftOne(List a) { + // Clone the list + List output = new ArrayList<>(a); + + Integer first = output.get(FIRST_POSITION); + output.remove(FIRST_POSITION); + output.add(first); + + return output; + } + + /** + * rotLeft. + */ + public static List rotLeft(List a, int d) { + + // Clone the list + List output = new ArrayList<>(a); + + int i = 1; + while (i <= d) { + output = rotLeftOne(output); + i += 1; + } + + return output; + } +} diff --git a/algorithm-exercises-java/src/test/java/ae/hackerrank/interview_preparation_kit/arrays/ArraysLeftRotationTest.java b/algorithm-exercises-java/src/test/java/ae/hackerrank/interview_preparation_kit/arrays/ArraysLeftRotationTest.java new file mode 100644 index 0000000..52f7bb2 --- /dev/null +++ b/algorithm-exercises-java/src/test/java/ae/hackerrank/interview_preparation_kit/arrays/ArraysLeftRotationTest.java @@ -0,0 +1,86 @@ +package ae.hackerrank.interview_preparation_kit.arrays; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.junit.jupiter.api.TestInstance.Lifecycle; + + +@TestInstance(Lifecycle.PER_CLASS) + class ArraysLeftRotationTest { + + public class ArraysLeftRotationTestCase { + public List input; + public List expected; + + public ArraysLeftRotationTestCase(List input, List expected) { + this.input = input; + this.expected = expected; + } + } + + public class ArraysLeftRotationsTestCase { + public List input; + public Integer d; + public List expected; + + public ArraysLeftRotationsTestCase(List input, Integer d, List expected) { + this.input = input; + this.d = d; + this.expected = expected; + } + } + + public List testCases; + public List testRotationsCases; + + @BeforeAll + public void setup() { + this.testCases = Arrays.asList( + new ArraysLeftRotationTestCase(Arrays.asList(1, 2, 3, 4, 5), Arrays.asList(2, 3, 4, 5, 1)), + new ArraysLeftRotationTestCase(Arrays.asList(2, 3, 4, 5, 1), Arrays.asList(3, 4, 5, 1, 2)), + new ArraysLeftRotationTestCase(Arrays.asList(3, 4, 5, 1, 2), Arrays.asList(4, 5, 1, 2, 3)), + new ArraysLeftRotationTestCase(Arrays.asList(4, 5, 1, 2, 3), Arrays.asList(5, 1, 2, 3, 4)), + new ArraysLeftRotationTestCase(Arrays.asList(5, 1, 2, 3, 4), Arrays.asList(1, 2, 3, 4, 5)) + ); + + this.testRotationsCases = Arrays.asList( + new ArraysLeftRotationsTestCase( + Arrays.asList(1, 2, 3, 4, 5), + 4, + Arrays.asList(5, 1, 2, 3, 4)) + ); + } + + @Test void testRotLeftOne() { + for (ArraysLeftRotationTestCase testCase : this.testCases) { + List solutionFound = ArraysLeftRotation.rotLeftOne(testCase.input); + + assertEquals(testCase.expected, solutionFound, + String.format("%s(%s) answer must be: %s", + "CompareTriplets.compareTriplets", + testCase.input.toString(), + testCase.expected.toString()) + ); + } + } + + @Test void testRotLeft() { + for (ArraysLeftRotationsTestCase testCase : this.testRotationsCases) { + List solutionFound = ArraysLeftRotation.rotLeft(testCase.input, testCase.d); + + assertEquals(testCase.expected, solutionFound, + String.format("%s(%s, %d) answer must be: %s", + "CompareTriplets.compareTriplets", + testCase.input.toString(), + testCase.d, + testCase.expected.toString()) + ); + } + } + +} diff --git a/config/checkstyle/checkstyle.xml b/config/checkstyle/checkstyle.xml index 429635d..9c23689 100644 --- a/config/checkstyle/checkstyle.xml +++ b/config/checkstyle/checkstyle.xml @@ -45,7 +45,7 @@ - + @@ -185,7 +185,7 @@ value="Type name ''{0}'' must match pattern ''{1}''."/> - + 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]