leet++Binary Search
EasyBinary searchArray

Binary Search

Create a function that locates a target in an ascending integer list. Return its position when present, or -1 when the target is absent.

Examples

Input: values = [-8, -1, 6, 14, 23], target = 14

Output: 3

The target is stored at position 3.

Input: values = [2, 9, 17], target = 4

Output: -1

The target does not occur in the list.

Constraints & goals

  • The input list is sorted in ascending order.
  • All values are distinct.
  • Aim for logarithmic search time.

Camera feed

Preview
Camera starts when the session begins
Read-only

Transcribed whiteboard text will appear here.