-
-
Notifications
You must be signed in to change notification settings - Fork 49.6k
[Searches] Fix Binary Search bug with duplicate elements #13946
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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>
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: