easy
Binary Search
Return the index of target in a strictly increasing integer array nums, or -1 if it is absent. Use logarithmic search time.
Constraints
- 0 ≤ n ≤ 100000
- Distinct sorted values between -10^9 and 10^9
Examples
in: [[1,3,7,12,19],7]
out: 2
Middle
in: [[1,3,7],5]
out: -1
Absent between values
Code it yourself
Solve in
Practice journal →Draft saved in this browser.
Public test cases · contract v1
Arguments are passed to your function. Tests are public practice checks, not hidden interview grading. Passing does not prove every possible input.
- Middle
[[1,3,7,12,19],7] → 2
- Absent between values
[[1,3,7],5] → -1
- Empty
[[],4] → -1
- Single match
[[4],4] → 0
- Above range
[[1,3],7] → -1
- Below range
[[1,3],-1] → -1
- Right boundary
[[1,3,7,12],12] → 3
Hints:
Which approach applies?
Choose an approach to check your pattern recognition, or reveal the discussion when you need help.