[Searches] Fix Binary Search bug with duplicate elements#13946
Conversation
|
Hi maintainers, this PR fixes issue #13886 and all tests + pre-commit checks are passing. Thank you! |
37fe148 to
ad1574f
Compare
There was a problem hiding this comment.
Pull request overview
This PR adds a new binary_search_with_duplicates function to handle binary search in sorted lists containing duplicate elements. The implementation uses lower_bound and upper_bound helper functions to find the range of all occurrences of the target value, addressing issue #13886.
Key changes:
- Adds
binary_search_with_duplicatesfunction that returns a list of all indices where the target occurs - Implements lower_bound and upper_bound as nested helper functions using binary search
- Includes doctests covering single occurrences, multiple duplicates, and not-found cases
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Added docstrings for lower_bound and upper_bound functions.
for more information, see https://pre-commit.ci
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated 6 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Updated docstring parameter and return type annotations for lower_bound and upper_bound functions.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated 3 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
[Searches] Fix Binary Search bug with duplicate elements (TheAlgorithms#13946)
Describe your change:
This PR fixes a bug where the Binary Search algorithm returned incorrect results (or failed) when the input list contained duplicate elements.
I have updated the logic to handle duplicates correctly and added doctests to verify the fix.
Fixes #13886
Checklist: