Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
54c214c
Add array rotation utility using reversal algorithm
Bhanubasyan May 13, 2026
cb3fea7
Fix checkstyle issues
Bhanubasyan May 13, 2026
8376bbe
Fix checkstyle issues
Bhanubasyan May 13, 2026
df2db03
Fix checkstyle issues
Bhanubasyan May 13, 2026
d729f7b
Fix checkstyle issues
Bhanubasyan May 13, 2026
e3d8cb1
Fix checkstyle issues
Bhanubasyan May 13, 2026
3fc7f45
Fix checkstyle issues
Bhanubasyan May 13, 2026
8831d24
Fix checkstyle issues
Bhanubasyan May 13, 2026
0940d5e
Fix checkstyle issues
Bhanubasyan May 13, 2026
4adc28c
Fix checkstyle issues
Bhanubasyan May 13, 2026
f28d8fc
Fix checkstyle issues
Bhanubasyan May 13, 2026
d1152ce
Fix checkstyle issues
Bhanubasyan May 13, 2026
52a57dd
Fix checkstyle issues
Bhanubasyan May 13, 2026
f81884d
Fix checkstyle issues
Bhanubasyan May 13, 2026
43731f0
Fix checkstyle issues
Bhanubasyan May 13, 2026
bb2df00
Fix checkstyle issues
Bhanubasyan May 13, 2026
a21bed0
Fix checkstyle issues
Bhanubasyan May 13, 2026
f9e24d0
Fix checkstyle issues
Bhanubasyan May 13, 2026
a18a7c8
Fix checkstyle issues
Bhanubasyan May 13, 2026
6a7d2c7
Fix checkstyle issues
Bhanubasyan May 13, 2026
d44a3f4
Merge branch 'master' into add-array-rotation-utility
Bhanubasyan May 13, 2026
c0746a5
Merge branch 'master' into add-array-rotation-utility
Bhanubasyan May 14, 2026
d0bca4f
Merge branch 'master' into add-array-rotation-utility
Bhanubasyan May 17, 2026
81d90cf
Add unit tests for ArrayRotation
Bhanubasyan May 17, 2026
af33548
Add unit tests for ArrayRotation
Bhanubasyan May 17, 2026
c11d0ca
Add unit tests for array rotation utility
Bhanubasyan May 17, 2026
0289397
Add unit tests for array rotation utility
Bhanubasyan May 17, 2026
1226614
Fix formatting in ArrayRotation tests
Bhanubasyan May 17, 2026
1f4bc30
Fix formatting in ArrayRotation tests
Bhanubasyan May 17, 2026
ba42f82
Fix formatting in ArrayRotation tests
Bhanubasyan May 17, 2026
2eb72d9
Fix formatting in ArrayRotation tests
Bhanubasyan May 17, 2026
5e67220
Fix formatting in ArrayRotation tests
Bhanubasyan May 17, 2026
47aa448
Fix formatting in ArrayRotation tests
Bhanubasyan May 17, 2026
c278b2d
Fix formatting in ArrayRotation tests
Bhanubasyan May 17, 2026
e6393ac
Fix formatting in ArrayRotation tests
Bhanubasyan May 17, 2026
cf56a80
Fix formatting in ArrayRotation tests
Bhanubasyan May 17, 2026
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
78 changes: 78 additions & 0 deletions src/main/java/com/thealgorithms/others/ArrayRotation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.thealgorithms.others;

/**
* Array Rotation Utility
*
* Supports:
* 1. Left Rotation
* 2. Right Rotation
*
* Approach:
* Reversal Algorithm
*
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
public final class ArrayRotation {

private ArrayRotation() {
}

/**
* Rotates the array to the right by k positions.
*
* @param nums the input array
* @param k number of rotations
*/
public static void rotateRight(int[] nums, int k) {
int n = nums.length;

if (n == 0) {
return;
}

k = k % n;

reverse(nums, 0, n - 1);
reverse(nums, 0, k - 1);
reverse(nums, k, n - 1);
}

/**
* Rotates the array to the left by k positions.
*
* @param nums the input array
* @param k number of rotations
*/
public static void rotateLeft(int[] nums, int k) {
int n = nums.length;

if (n == 0) {
return;
}

k = k % n;

reverse(nums, 0, k - 1);
reverse(nums, k, n - 1);
reverse(nums, 0, n - 1);
}

/**
* Reverses elements between start and end indices.
*
* @param nums the input array
* @param start starting index
* @param end ending index
*/
private static void reverse(int[] nums, int start, int end) {
while (start < end) {
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;

start++;
end--;
}
}
}
62 changes: 62 additions & 0 deletions src/main/java/com/thealgorithms/others/ArrayRotationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.thealgorithms.others;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;

import org.junit.jupiter.api.Test;

public class ArrayRotationTest {

@Test
void shouldRotateArrayRightByTwoPositions() {
int[] values = {1, 2, 3, 4, 5};

ArrayRotation.rotateRight(values, 2);

assertArrayEquals(new int[] {4, 5, 1, 2, 3}, values);
}

@Test
void shouldRotateArrayLeftByTwoPositions() {
int[] values = {1, 2, 3, 4, 5};

ArrayRotation.rotateLeft(values, 2);

assertArrayEquals(new int[] {3, 4, 5, 1, 2}, values);
}

@Test
void shouldHandleRotationGreaterThanArrayLength() {
int[] values = {10, 20, 30, 40};

ArrayRotation.rotateRight(values, 6);

assertArrayEquals(new int[] {30, 40, 10, 20}, values);
}

@Test
void shouldKeepSingleElementArrayUnchanged() {
int[] values = {99};

ArrayRotation.rotateLeft(values, 5);

assertArrayEquals(new int[] {99}, values);
}

@Test
void shouldHandleEmptyArrayWithoutErrors() {
int[] values = {};

ArrayRotation.rotateRight(values, 3);

assertArrayEquals(new int[] {}, values);
}

@Test
void shouldReturnOriginalArrayWhenRotationIsZero() {
int[] values = {7, 8, 9};

ArrayRotation.rotateLeft(values, 0);

assertArrayEquals(new int[] {7, 8, 9}, values);
}
}
62 changes: 62 additions & 0 deletions src/test/java/com/thealgorithms/others/ArrayRotationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.thealgorithms.others;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;

import org.junit.jupiter.api.Test;

public class ArrayRotationTest {

@Test
void shouldRotateArrayRightByTwoPositions() {
int[] values = {1, 2, 3, 4, 5};

ArrayRotation.rotateRight(values, 2);

assertArrayEquals(new int[] {4, 5, 1, 2, 3}, values);
}

@Test
void shouldRotateArrayLeftByTwoPositions() {
int[] values = {1, 2, 3, 4, 5};

ArrayRotation.rotateLeft(values, 2);

assertArrayEquals(new int[] {3, 4, 5, 1, 2}, values);
}

@Test
void shouldHandleRotationGreaterThanArrayLength() {
int[] values = {10, 20, 30, 40};

ArrayRotation.rotateRight(values, 6);

assertArrayEquals(new int[] {30, 40, 10, 20}, values);
}

@Test
void shouldKeepSingleElementArrayUnchanged() {
int[] values = {99};

ArrayRotation.rotateLeft(values, 5);

assertArrayEquals(new int[] {99}, values);
}

@Test
void shouldHandleEmptyArrayWithoutErrors() {
int[] values = {};

ArrayRotation.rotateRight(values, 3);

assertArrayEquals(new int[] {}, values);
}

@Test
void shouldReturnOriginalArrayWhenRotationIsZero() {
int[] values = {7, 8, 9};

ArrayRotation.rotateLeft(values, 0);

assertArrayEquals(new int[] {7, 8, 9}, values);
}
}
Loading