Skip to content

Add array rotation utility - #7586

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

Bhanubasyan wants to merge 46 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 28.57143% with 55 lines in your changes missing coverage. Please review.
✅ Project coverage is 80.45%. Comparing base (6ec6339) to head (5f35122).
⚠️ Report is 17 commits behind head on master.

Files with missing lines Patch % Lines
src/main/java/com/thealgorithms/others/Trie.java 0.00% 53 Missing ⚠️
...n/java/com/thealgorithms/others/ArrayRotation.java 91.66% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7586      +/-   ##
============================================
- Coverage     80.61%   80.45%   -0.17%     
- Complexity     7497     7503       +6     
============================================
  Files           816      818       +2     
  Lines         24097    24174      +77     
  Branches       4741     4755      +14     
============================================
+ Hits          19427    19449      +22     
- Misses         3907     3961      +54     
- Partials        763      764       +1     

☔ 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.

import java.util.ArrayList;
import java.util.List;

public class Trie {

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 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.

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?

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.

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