DSA Tracker

Easy

Lower Bound and Upper Bound

An easy Binary Search problem included in Striver A2Z. Below: the roles whose interviews prioritise this topic, and how to practise it.

Topic
Binary Search
Sheets
1
Core for
7 roles
Platform
GeeksforGeeks

The problem

Given a sorted array and a target value, find the lower bound — the index of the first element that is greater than or equal to the target. If all elements are less than the target, return the array length.

Example 1

Input
arr = [1,2,3,4,5], target = 3
Output
2
Why
The first element >= 3 is at index 2 (value 3).

Example 2

Input
arr = [1,2,3,4,5], target = 6
Output
5
Why
All elements are less than 6, so return the array length 5.

Example 3

Input
arr = [1,2,2,3,3,5,6], target = 2
Output
1
Why
The first element >= 2 is at index 1.

Constraints

  • 1 <= arr.length <= 10^5
  • -10^9 <= arr[i], target <= 10^9
  • arr is sorted in non-decreasing order

How to think about it

Updated 2026-09-09

The condition arr[i] >= target partitions the sorted array into false on the left and true on the right. Finding the lower bound is locating the very first index where the predicate becomes true, discarding half the candidates on each step.

Approaches, worst first

  1. Linear threshold scan

    time O(n) · space O(1)

    Iterate from index 0 until finding the first item satisfying arr[i] >= target. Correct but fails to leverage sorted order, scanning up to 10^5 items.

  2. Lower-bound binary searchWrite this one

    time O(log n) · space O(1)

    Maintain search interval [low, high] initialized to [0, n - 1] and keep ans = n. Whenever arr[mid] >= target, record mid in ans and contract search left to mid - 1; otherwise shift right to mid + 1.

Where people lose marks · 3
  • Returning -1 when target exceeds all elements instead of the specified default value of array length n.
  • Using arr[mid] > target instead of arr[mid] >= target locates the upper bound instead of the lower bound, skipping exact matches.
  • Setting high = mid with loop condition low <= high causes an infinite loop when low equals high.

The theory behind it

Binary Search — the ground this problem stands on. All Binary Search problems

What Binary Search is

Binary search is the guessing strategy used when searching a thick telephone directory or guessing a secret number between one and a hundred. Rather than inspecting names one by one from the first page, the search opens straight to the middle page. If the target precedes that middle entry, the entire back half is discarded; if it follows, the front half is eliminated. Repeating this halved split finds the item with remarkable speed.

When to reach for it

Reach for binary search when queries target sorted collections, rotated sorted arrays, or monotonic answer spaces. Strong hints include logarithmic time constraints like O(log n) or search ranges exceeding one billion where stepping one value at a time times out. It also applies when validating whether a guessed solution is possible via a monotonic boolean check, known as binary search on answer.

How the pattern works

Define the search territory with two inclusive pointers, low and high. Compute the midpoint using low plus half the difference to high, avoiding integer overflow. Formulate an exact boolean condition that divides the range into true and false halves. Decide whether the boundary condition includes the midpoint or shifts strictly past it. The loop invariant states that the sought target, if it exists, remains trapped inside the interval throughout every iteration.

What each operation costs

OperationTime
find target in sorted arrayO(log n)
find boundary in monotonic rangeO(log n)
What usually goes wrong with Binary Search
  • Triggering integer overflow by computing middle using low plus high divided by two instead of low plus half of high minus low in fixed-width numeric types.
  • Creating an infinite loop when the interval shrinks to two elements by setting low equal to mid when mid was rounded down.
  • Mismatched loop condition and bounds updates, such as pairing low less than or equal to high with non-advancing pointer assignments that never terminate.

Which roles need this problem

Binary Search is a core topic for these 7 roles — if you're targeting one of them, this problem is early in your path, not optional.

Secondary for 17 more roles, including Full-Stack Developer, Data Engineer, Android Developer.

Track this in your role's order

Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.

Start free

More Binary Search problems

Problem set and role mapping as of .