← All problems

Binary Search (classic)

Easy searching LeetCode ↗
Given a sorted integer array and a target value, return the target's index, or -1 if it is absent. Implement binary search iteratively and handle the midpoint computation safely.

Hints (0/3 revealed)

Log in to reveal hints progressively.

Complexity

Time O(log n), Space O(1)

Common pitfalls

  • (lo+hi)//2 overflow in fixed-width languages
  • Infinite loop from wrong bound updates

Your solution

Log in to save your code and track progress.