mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-11 22:22:21 +08:00
Feature/4638 array right rotation (#5014)
* Create ArrayRightRotationTest.java * Create ArrayRightRotation.java * The updated one * The updated one * Added the test cases * Added new test cases! * Update ArrayRightRotation.java * Update ArrayRightRotationTest.java
This commit is contained in:

committed by
GitHub

parent
19b7a22ec9
commit
8804cec957
@ -0,0 +1,28 @@
|
|||||||
|
package com.thealgorithms.others;
|
||||||
|
|
||||||
|
public class ArrayRightRotation {
|
||||||
|
public static int[] rotateRight(int[] arr, int k) {
|
||||||
|
if (arr == null || arr.length == 0 || k < 0) {
|
||||||
|
throw new IllegalArgumentException("Invalid input");
|
||||||
|
}
|
||||||
|
|
||||||
|
int n = arr.length;
|
||||||
|
k = k % n; // Handle cases where k is larger than the array length
|
||||||
|
|
||||||
|
reverseArray(arr, 0, n - 1);
|
||||||
|
reverseArray(arr, 0, k - 1);
|
||||||
|
reverseArray(arr, k, n - 1);
|
||||||
|
|
||||||
|
return arr;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void reverseArray(int[] arr, int start, int end) {
|
||||||
|
while (start < end) {
|
||||||
|
int temp = arr[start];
|
||||||
|
arr[start] = arr[end];
|
||||||
|
arr[end] = temp;
|
||||||
|
start++;
|
||||||
|
end--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,53 @@
|
|||||||
|
package com.thealgorithms.others;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class ArrayRightRotationTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testArrayRightRotation() {
|
||||||
|
int[] arr = {1, 2, 3, 4, 5, 6, 7};
|
||||||
|
int k = 3;
|
||||||
|
int[] expected = {5, 6, 7, 1, 2, 3, 4};
|
||||||
|
int[] result = ArrayRightRotation.rotateRight(arr, k);
|
||||||
|
assertArrayEquals(expected, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testArrayRightRotationWithZeroSteps() {
|
||||||
|
int[] arr = {1, 2, 3, 4, 5, 6, 7};
|
||||||
|
int k = 0;
|
||||||
|
int[] expected = {1, 2, 3, 4, 5, 6, 7};
|
||||||
|
int[] result = ArrayRightRotation.rotateRight(arr, k);
|
||||||
|
assertArrayEquals(expected, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testArrayRightRotationWithEqualSizeSteps() {
|
||||||
|
int[] arr = {1, 2, 3, 4, 5, 6, 7};
|
||||||
|
int k = arr.length;
|
||||||
|
int[] expected = {1, 2, 3, 4, 5, 6, 7};
|
||||||
|
int[] result = ArrayRightRotation.rotateRight(arr, k);
|
||||||
|
assertArrayEquals(expected, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testArrayRightRotationWithLowerSizeSteps() {
|
||||||
|
int[] arr = {1, 2, 3, 4, 5, 6, 7};
|
||||||
|
int k = 2;
|
||||||
|
int[] expected = {6, 7, 1, 2, 3, 4, 5};
|
||||||
|
int[] result = ArrayRightRotation.rotateRight(arr, k);
|
||||||
|
assertArrayEquals(expected, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testArrayRightRotationWithHigherSizeSteps() {
|
||||||
|
int[] arr = {1, 2, 3, 4, 5, 6, 7};
|
||||||
|
int k = 10;
|
||||||
|
int[] expected = {5, 6, 7, 1, 2, 3, 4};
|
||||||
|
int[] result = ArrayRightRotation.rotateRight(arr, k);
|
||||||
|
assertArrayEquals(expected, result);
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user