Skip to content

fix(searches): fix out-of-bounds in single-row and single-column in BinarySearch2dArray - #7724

Merged
DenizAltunkapan merged 2 commits into
TheAlgorithms:masterfrom
hardikkaurani:fix/binary-search-2d-array-bounds
Nov 5, 2026
Merged

DenizAltunkapan merged 2 commits into
TheAlgorithms:masterfrom
hardikkaurani:fix/binary-search-2d-array-bounds

Conversation

@hardikkaurani

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

Description

This pull request fixes out-of-bounds errors on single-row and single-column matrices, and exposes binarySearch as a public method in BinarySearch2dArray:

  1. Single-Row Upper Bound Fix: In binarySearch(int[][] arr, int target), when rowCount == 1, the helper binarySearch(arr, target, 0, 0, colCount) was called with colCount instead of colCount - 1. Because the helper uses an inclusive upper bound (colStart <= colEnd), searching for an element greater than arr[0][colCount - 1] caused midIndex to equal colCount, triggering an ArrayIndexOutOfBoundsException. Updated to pass colCount - 1.
  2. Single-Column Handling: When colCount == 1, midCol is 0. Accessing midCol - 1 threw ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 1. Added an edge-case guard returning {-1, -1} when colCount == 1 after checking midCol.
  3. Public API Visibility: Changed binarySearch(int[][] arr, int target) from package-private to public static.
  4. Expanded Tests: Added unit tests covering single-row searches where target is absent (greater than last, smaller than first), single-column searches (present and absent), and single-element 2D arrays.

Copilot AI balanced review requested due to automatic review settings November 2, 2026 05:58

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@hardikkaurani

hardikkaurani commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor Author

Hi @DenizAltunkapan,

Could you please review this pull request when you have a moment?

Summary of Changes

  • Single-Row Bound Fix: Updated binarySearch(arr, target, 0, 0, colCount) to pass colCount - 1, since colEnd is inclusive (colStart <= colEnd). This prevents an ArrayIndexOutOfBoundsException when searching for an element greater than the last element in a single-row array.
  • Single-Column Matrix Handling: Added a guard that returns {-1, -1} when colCount == 1, after checking midCol, preventing negative index access through midCol - 1.
  • Visibility: Changed binarySearch(int[][] arr, int target) to public static.
  • Test Coverage: Added tests for single-row arrays with absent elements, single-column matrices, and single-element matrices. All 17 unit tests pass.

Thank you for your time and consideration!

@codecov-commenter

codecov-commenter commented Nov 2, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.29%. Comparing base (51a0156) to head (41b3d52).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7724      +/-   ##
============================================
+ Coverage     82.27%   82.29%   +0.01%     
- Complexity     8410     8412       +2     
============================================
  Files           843      843              
  Lines         26144    26146       +2     
  Branches       5102     5103       +1     
============================================
+ Hits          21510    21516       +6     
+ Misses         3842     3841       -1     
+ Partials        792      789       -3     

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

@DenizAltunkapan
DenizAltunkapan enabled auto-merge (squash) November 5, 2026 18:23
@DenizAltunkapan
DenizAltunkapan merged commit 0a76bf8 into TheAlgorithms:master Nov 5, 2026
7 checks passed
@hardikkaurani

Copy link
Copy Markdown
Contributor Author

Thanks @DenizAltunkapan

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.

4 participants