-
Notifications
You must be signed in to change notification settings - Fork 20k
Feature/4638 array right rotation #5014
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
BamaCharanChhandogi
merged 8 commits into
TheAlgorithms:master
from
SarthakChaudhary46:Feature/4638-ArrayRightRotation
Jan 13, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b7c7356
Create ArrayRightRotationTest.java
SarthakChaudhary46 b8ee065
Create ArrayRightRotation.java
SarthakChaudhary46 e66ec01
The updated one
SarthakChaudhary46 f88c3dc
The updated one
SarthakChaudhary46 a1a935f
Added the test cases
SarthakChaudhary46 7b5845d
Added new test cases!
SarthakChaudhary46 f22648a
Update ArrayRightRotation.java
SarthakChaudhary46 a21da28
Update ArrayRightRotationTest.java
SarthakChaudhary46 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
src/test/java/com/thealgorithms/others/ArrayRightRotation.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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--; | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
src/test/java/com/thealgorithms/others/ArrayRightRotationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.