Skip to content

fix: stop BinarySearch2dArray crashing on two-column matrices - #7789

Merged
DenizAltunkapan merged 1 commit into
TheAlgorithms:masterfrom
Aditya2375:fix/binary-search-2d-two-column-crash
Nov 30, 2026
Merged

DenizAltunkapan merged 1 commit into
TheAlgorithms:masterfrom
Aditya2375:fix/binary-search-2d-two-column-crash

Conversation

@Aditya2375

Copy link
Copy Markdown
Contributor

Describe your change:

Fixed BinarySearch2dArray.binarySearch throwing ArrayIndexOutOfBoundsException on any matrix with exactly two columns when the target is not found in the middle column.

The method compares the target against the middle column (midCol = colCount / 2) to pick a row band, then searches the right part of the candidate row via arr[startRow][midCol + 1]. For a two-column matrix midCol is 1, which is already the last column, so midCol + 1 reads past the end of the row and the search crashes - including for targets that are present in the matrix:

BinarySearch2dArray.binarySearch(new int[][] {{9, 23}, {26, 30}}, 26);
// ArrayIndexOutOfBoundsException: Index 2 out of bounds for length 2
// 26 is in the matrix at [1][0]

The existing tests only use four-column and single-column matrices, which is why this survived.

The fix guards the right-side branch with midCol + 1 < colCount. When there is no column to the right of the middle column, the search falls through to the endRow checks, which route the target to the correct row segment (or report [-1, -1] when it is absent). No other path changes: for three or more columns the guard is always true.

I verified the fix by differential testing against a brute-force scan on 200,000 randomized matrices (1-7 rows/columns, sorted with duplicates, targets both present and absent): zero crashes, zero mismatched results. Added regression tests for the two-column cases (present target, absent target, larger matrix) and a value-gap case; they fail on the current code and pass with the fix. All 21 tests in BinarySearch2dArrayTest pass.

Checklist:

  • 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

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.58%. Comparing base (21a2335) to head (27c8eee).

Additional details and impacted files
@@             Coverage Diff              @@
##             master    #7789      +/-   ##
============================================
- Coverage     82.58%   82.58%   -0.01%     
  Complexity     8604     8604              
============================================
  Files           848      848              
  Lines         26460    26460              
  Branches       5181     5181              
============================================
- Hits          21853    21852       -1     
- Misses         3823     3824       +1     
  Partials        784      784              

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

@Aditya2375
Aditya2375 marked this pull request as ready for review November 29, 2026 19:17
@DenizAltunkapan
DenizAltunkapan merged commit 14cbe56 into TheAlgorithms:master Nov 30, 2026
7 checks passed
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