Skip to content

Commit 7cc37a4

Browse files
Rotate Array by right
1 parent f4d1c9b commit 7cc37a4

3 files changed

Lines changed: 110 additions & 0 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package array.rotate_array;
2+
3+
/**
4+
* Utility class to rotate an integer array to the right by a given number of steps.
5+
*
6+
* <p>The rotation is performed in-place using the array reversal technique,
7+
* achieving O(n) time complexity and O(1) extra space.</p>
8+
*/
9+
public class RotateArray {
10+
11+
/**
12+
* Rotates the given array to the right by {@code k} steps.
13+
*
14+
* <p>If {@code k} is greater than the array length, it is reduced using
15+
* the modulo operation.</p>
16+
*
17+
* @param nums the array to be rotated
18+
* @param k the number of steps to rotate the array to the right;
19+
* must be non-negative
20+
* @throws ArithmeticException if the array is empty
21+
*/
22+
public void rotate(int[] nums, int k) {
23+
k = k % nums.length;
24+
reverse(nums, 0, nums.length - k - 1);
25+
reverse(nums, nums.length - k, nums.length - 1);
26+
reverse(nums, 0, nums.length - 1);
27+
}
28+
29+
/**
30+
* Reverses the elements of the array between the given indices.
31+
*
32+
* @param nums the array whose elements are to be reversed
33+
* @param startIdx the starting index of the range (inclusive)
34+
* @param lastIdx the ending index of the range (inclusive)
35+
*/
36+
private void reverse(int[] nums, int startIdx, int lastIdx) {
37+
while (startIdx < lastIdx) {
38+
int temp = nums[startIdx];
39+
nums[startIdx] = nums[lastIdx];
40+
nums[lastIdx] = temp;
41+
startIdx++;
42+
lastIdx--;
43+
}
44+
}
45+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Provides utilities for performing array rotation operations.
3+
*
4+
* <p>This package contains classes that implement efficient algorithms
5+
* to rotate an integer array to the right by a given number of steps.
6+
* The rotation is performed in-place using constant extra space.</p>
7+
*
8+
* <p>Key concepts used:
9+
* <ul>
10+
* <li>Modulo operation to handle large rotation counts</li>
11+
* <li>Array reversal technique for optimal performance</li>
12+
* </ul>
13+
* </p>
14+
*
15+
* @since 1.0
16+
*/
17+
package array.rotate_array;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package array.rotate_array;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
6+
7+
/**
8+
* Test class for {@link RotateArray}.
9+
*/
10+
class RotateArrayTest {
11+
12+
private final RotateArray rotateArray = new RotateArray();
13+
14+
@Test
15+
void testRotateByNormalK() {
16+
int[] nums = {1, 2, 3, 4, 5, 6, 7};
17+
rotateArray.rotate(nums, 3);
18+
assertArrayEquals(new int[]{5, 6, 7, 1, 2, 3, 4}, nums);
19+
}
20+
21+
@Test
22+
void testRotateByZero() {
23+
int[] nums = {1, 2, 3};
24+
rotateArray.rotate(nums, 0);
25+
assertArrayEquals(new int[]{1, 2, 3}, nums);
26+
}
27+
28+
@Test
29+
void testRotateByArrayLength() {
30+
int[] nums = {1, 2, 3, 4};
31+
rotateArray.rotate(nums, 4);
32+
assertArrayEquals(new int[]{1, 2, 3, 4}, nums);
33+
}
34+
35+
@Test
36+
void testRotateByMoreThanArrayLength() {
37+
int[] nums = {1, 2, 3};
38+
rotateArray.rotate(nums, 5);
39+
assertArrayEquals(new int[]{2, 3, 1}, nums);
40+
}
41+
42+
@Test
43+
void testSingleElementArray() {
44+
int[] nums = {10};
45+
rotateArray.rotate(nums, 3);
46+
assertArrayEquals(new int[]{10}, nums);
47+
}
48+
}

0 commit comments

Comments
 (0)