Skip to content

Feature/array left rotation #178

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<Integer> rotLeftOne(List<Integer> a) {
// Clone the list
List<Integer> output = new ArrayList<>(a);

Integer first = output.get(FIRST_POSITION);
output.remove(FIRST_POSITION);
output.add(first);

return output;
}

/**
* rotLeft.
*/
public static List<Integer> rotLeft(List<Integer> a, int d) {

// Clone the list
List<Integer> output = new ArrayList<>(a);

int i = 1;
while (i <= d) {
output = rotLeftOne(output);
i += 1;
}

return output;
}
}
Original file line number Diff line number Diff line change
@@ -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<Integer> input;
public List<Integer> expected;

public ArraysLeftRotationTestCase(List<Integer> input, List<Integer> expected) {
this.input = input;
this.expected = expected;
}
}

public class ArraysLeftRotationsTestCase {
public List<Integer> input;
public Integer d;
public List<Integer> expected;

public ArraysLeftRotationsTestCase(List<Integer> input, Integer d, List<Integer> expected) {
this.input = input;
this.d = d;
this.expected = expected;
}
}

public List<ArraysLeftRotationTestCase> testCases;
public List<ArraysLeftRotationsTestCase> 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<Integer> 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<Integer> 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())
);
}
}

}
4 changes: 2 additions & 2 deletions config/checkstyle/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<module name="LineLength">
<property name="fileExtensions" value="java"/>
<property name="max" value="100"/>
<property name="ignorePattern" value="^package.*|^import.*|a href|href|http://|https://|ftp://"/>
<property name="ignorePattern" value="^package.*|^import.*|a href|href|http://|https://|ftp://|[[*]]"/>
</module>

<module name="TreeWalker">
Expand Down Expand Up @@ -185,7 +185,7 @@
value="Type name ''{0}'' must match pattern ''{1}''."/>
</module>
<module name="MemberName">
<property name="format" value="^[_]*[a-z][a-z0-9][a-zA-Z0-9]*$"/>
<property name="format" value="^[_]*[a-z][a-zA-Z0-9]*$"/>
<message key="name.invalidPattern"
value="Member name ''{0}'' must match pattern ''{1}''."/>
</module>
Expand Down
Original file line number Diff line number Diff line change
@@ -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]