WorkspaceProblem lab
ALGORITHM VISUALIZER

Binary Search

Opening the interactive workspace…

Understanding Binary Search

Given a strictly increasing integer array nums and a target, return the target's index, or −1 if it is absent.

How it works

Sorted order lets each comparison eliminate an entire half of the remaining search space.

Keep an inclusive [left, right] interval. If the midpoint is too small, discard it and everything to its left; otherwise discard the right half.

Try an example

Input: nums = [-1, 0, 3, 5, 9, 12], target = 9

Output: 4

The value 9 is at index 4.

Constraints

  • The array must be sorted in strictly increasing order.
  • An empty array returns −1.
  • Visualization limit: 64 integers.

Use the simulator

Change the input, then choose Visualize input. Use Next and Previous to inspect each decision, or Play to advance automatically. Compare approaches and their complexity. Choose Try yourself to write JavaScript, select My code and Visualize code to inspect its execution, or Run Tests to check results.

Execution happens in your browser. Drafts are saved locally; avoid entering secrets or sensitive information.

Related concepts and complexity