Repository navigation
Add array rotation utility - #7586
Bhanubasyan wants to merge 55 commits into
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #7586 +/- ##
============================================
+ Coverage 82.00% 82.01% +0.01%
- Complexity 8280 8286 +6
============================================
Files 840 841 +1
Lines 25872 25896 +24
Branches 5053 5055 +2
============================================
+ Hits 21216 21239 +23
- Misses 3866 3867 +1
Partials 790 790 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
| return; | ||
| } | ||
|
|
||
| k = k % n; |
There was a problem hiding this comment.
What should happen when k is negative? In Java, % keeps the sign of the dividend, so a negative k can result in invalid indices during reverse(). Could we either normalize k to the range [0, n) or explicitly reject negative values, and add a test for this case?
There was a problem hiding this comment.
Handled negative k by normalizing it to the range [0, n) and added a test case covering negative rotation.
| void shouldRotateArrayLeftByTwoPositions() { | ||
| int[] values = {1, 2, 3, 4, 5}; | ||
|
|
||
| ArrayRotation.rotateLeft(values, 2); |
There was a problem hiding this comment.
Could we also add a test for rotateLeft() when k is greater than the array length? The current test only covers this case for right rotation, while left rotation has separate logic.
There was a problem hiding this comment.
Added a separate test for rotateLeft() when k is greater than the array length.
|
Hi Mohith,
Thank you for the detailed review and suggestions.
I agree with all three points:
-
I’ll remove Trie.java from this PR and keep the PR focused specifically
on the array rotation utility.
-
I’ll handle negative k by normalizing it to the valid range and add a
corresponding test case.
-
I’ll add a test covering rotateLeft() when k is greater than the array
length.
I’ll make these changes and update the PR accordingly.
Thanks again for the review!
…On Mon, 5 Oct 2026 at 11:10, Mohith Vardhan Beedupalli < ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In src/main/java/com/thealgorithms/others/Trie.java
<#7586 (comment)>:
> @@ -0,0 +1,162 @@
+package com.thealgorithms.others;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public class Trie {
Could we remove Trie.java from this PR? This PR is focused on adding the
array rotation utility, but the Trie implementation seems unrelated to the
main purpose of the change. Keeping it here makes the PR larger and also
introduces additional code that doesn't have corresponding tests. I think
it would be cleaner to move the Trie changes into a separate PR.
------------------------------
In src/main/java/com/thealgorithms/others/ArrayRotation.java
<#7586 (comment)>:
> + }
+
+ /**
+ * 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;
What should happen when k is negative? In Java, % keeps the sign of the
dividend, so a negative k can result in invalid indices during reverse().
Could we either normalize k to the range [0, n) or explicitly reject
negative values, and add a test for this case?
------------------------------
In src/test/java/com/thealgorithms/others/ArrayRotationTest.java
<#7586 (comment)>:
> +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);
Could we also add a test for rotateLeft() when k is greater than the array
length? The current test only covers this case for right rotation, while
left rotation has separate logic.
—
Reply to this email directly, view it on GitHub
<#7586?email_source=notifications&email_token=BCOZJDD7V65UGJ33Q2HTC3D5SMX33A5CNFSNUABKM5UWIORPF5TWS5BNNB2WEL2QOVWGYUTFOF2WK43UKJSXM2LFO4XTKNBQHE4TMMRYG432M4TFMFZW63VGMF2XI2DPOKSWK5TFNZ2KYZTPN52GK4S7MNWGSY3L#pullrequestreview-5409962877>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/BCOZJDBPFYQJU5GIDXCH4AT5SMX33AVCNFSNUABEKJSXA33TNF2G64TZHM3DGNBXG43DMMB3JFZXG5LFHM2TEOBXGMYTSOJTGSQXMAQ>
.
Triage notifications, keep track of coding agent tasks and review pull
requests on the go with GitHub Mobile for iOS
<https://github.com/notifications/mobile/ios/BCOZJDAEYMZ2TPQDDPAXZND5SMX33A5CNFSNUABKM5UWIORPF5TWS5BNNB2WEL2QOVWGYUTFOF2WK43UKJSXM2LFO4XTKNBQHE4TMMRYG432M4TFMFZW63VGMF2XI2DPOKSWK5TFNZ2KUZTPN52GK4S7NFXXG>
and Android
<https://github.com/notifications/mobile/android/BCOZJDEPCOOFIHC4AYKT7P35SMX33A5CNFSNUABKM5UWIORPF5TWS5BNNB2WEL2QOVWGYUTFOF2WK43UKJSXM2LFO4XTKNBQHE4TMMRYG432M4TFMFZW63VGMF2XI2DPOKSWK5TFNZ2K4ZTPN52GK4S7MFXGI4TPNFSA>.
Download it today!
You are receiving this because you authored the thread.Message ID:
<TheAlgorithms/Java/pull/7586/review/5409962877 ***@***.***
com>
|
mohithvardhan002
left a comment
There was a problem hiding this comment.
Looks good. Logic is correct, tests cover the edge cases, and the earlier review comment about left rotation with k > n is addressed. Thanks!
clang-format -i --style=file path/to/your/file.java