Skip to content

Add array rotation utility - #7586

Open
Bhanubasyan wants to merge 55 commits into
TheAlgorithms:masterfrom
Bhanubasyan:add-array-rotation-utility
Open

Bhanubasyan wants to merge 55 commits into
TheAlgorithms:masterfrom
Bhanubasyan:add-array-rotation-utility

Conversation

@Bhanubasyan

@Bhanubasyan Bhanubasyan commented Aug 29, 2026 •

Copy link
Copy Markdown
Contributor
  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized it.
  • All filenames are in PascalCase.
  • All functions and variable names follow Java naming conventions.
  • All new algorithms have a URL in their comments that points to Wikipedia or other similar explanations.
  • All new algorithms include a corresponding test class that validates their functionality.
  • All new code is formatted with clang-format -i --style=file path/to/your/file.java

@codecov-commenter

codecov-commenter commented Aug 29, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 91.66667% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.01%. Comparing base (9d9e348) to head (ced68c8).

Files with missing lines Patch % Lines
...n/java/com/thealgorithms/others/ArrayRotation.java 91.66% 1 Missing and 1 partial ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread src/main/java/com/thealgorithms/others/Trie.java Outdated
return;
}

k = k % n;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a separate test for rotateLeft() when k is greater than the array length.

@Bhanubasyan

Bhanubasyan commented Oct 7, 2026 via email

Copy link
Copy Markdown
Contributor Author

@mohithvardhan002 mohithvardhan002 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Logic is correct, tests cover the edge cases, and the earlier review comment about left rotation with k > n is addressed. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants