close
Skip to content

Binary Search gives wrong output for duplicate elements #13886

@ABHIRANJAN-KUMAR1

Description

@ABHIRANJAN-KUMAR1

Repository commit

a0b0f41ae134aa1772d33bb930e5a960f9979e8

Python version (python --version)

Python 3.10.7

Dependencies version (pip freeze)

appnope==0.1.3
asttokens==2.0.8
backcall==0.2.0
decorator==5.1.1
executing==0.8.3
matplotlib-inline==0.1.6
parso==0.8.3
pexpect==4.8.0
pickleshare==0.7.5
prompt-toolkit==3.0.36
ptyprocess==0.7.0
pure-eval==0.2.2
Pygments==2.14.0
six==1.16.0
stack-data==0.2.0
traitlets==5.1.1
wcwidth==0.2.5

Expected behavior

The binary search function should correctly return the index of the target element, even if there are duplicate values in the list.
Example: For input [1, 2, 2, 2, 3] and target = 2, it should return any valid index such as 1, 2, or 3.

Actual behavior

The function returns -1 or sometimes the wrong index when duplicate values exist in the list.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions