Ceil in a Sorted Array

easy

You are given a sorted array arr and an integer x. Your task is to find the ceil of x — the smallest element in arr that is greater than or equal to x — and return its 0-based index.

If no element in arr is greater than or equal to x, return -1.

Note: if the ceil value appears multiple times, return the index of its first occurrence.

Hints

The array is sorted — what does one comparison against a middle element tell you about everything on either side of it?
Every element is either < x or >= x, and the sorted order groups them into two zones. You are hunting for the index where the second zone begins.
Binary search with a twist: when arr[mid] >= x, don't stop — record mid as a candidate and continue searching left with hi = mid - 1. The last recorded candidate is the first occurrence.

Common doubts

The smallest element of arr that is greater than or equal to x. If x itself is present, x is its own ceil. You return the index of that element, not the value.
Multiple copies of the ceil value can exist (e.g. [1, 1, 2] with x = 0 — both 1s qualify). The problem pins down a unique answer by asking for the leftmost index, which the sorted order makes well-defined.
Only when x is strictly greater than the last (largest) element — then nothing in the array is >= x, so no ceil exists.
Almost. The lower-bound index is exactly the insert position that keeps arr sorted (inserting before duplicates). The only difference is the out-of-range case: insert position would be n, while this problem maps it to -1.

Interview follow-ups

Mirror the loop: when arr[mid] <= x, record mid and search right (lo = mid + 1); otherwise search left. It is the same boundary hunt approached from the other side.
Run lower bound twice: first occurrence of v and first occurrence of v + 1 (or first index with element > v). The difference of the two indices is the count — two O(log n) searches.
Plain lower bound breaks because the two-zone structure is displaced. First binary search for the rotation point (the minimum), then run the ceil search in the correct half — still O(log n) overall.

Fun facts

  • The record-and-go-left loop you wrote here is exactly what C++ ships as std::lower_bound, Python as bisect.bisect_left, and Go as sort.SearchInts — one of the few interview algorithms that exists verbatim in every major standard library.
  • The 'find the first element that satisfies a condition' pattern — binary search on answer — powers far harder problems too: first bad version, allocating minimum pages, and capacity-to-ship-packages all reuse this identical loop with a fancier condition.
  • A subtle detail worth bragging about in interviews: writing mid = lo + (hi - lo) / 2 instead of (lo + hi) / 2 avoids integer overflow when lo and hi are both huge — a real bug that lived in Java's standard library binary search for nine years.

Asked at

AmazonMicrosoftAdobeOraclePaytm
Frequently Sometimes Occasionally
Example 1
Input: arr = [1, 2, 8, 10, 11, 12, 19], x = 5
Output: 2
The smallest element >= 5 is 8, at index 2.
Example 2
Input: arr = [1, 2, 8, 10, 11, 12, 19], x = 20
Output: -1
No element is >= 20, so there is no ceil.
Example 3
Input: arr = [1, 1, 2, 8, 10, 11, 12, 19], x = 0
Output: 0
The smallest element >= 0 is 1, which appears at indices 0 and 1. The first occurrence is index 0.
Constraints

- 1 <= arr.size <= 10^6 - 1 <= arr[i] <= 10^6 - 0 <= x <= 10^6 - arr is sorted in non-decreasing order

Solve this problem →