Ceil in a Sorted Array
Ceil in a Sorted Array is a easy binary search problem solved with the bisect pattern.
The best approach, optimal (bisect_left), runs in O(log n) time and O(1) space.
Below are 2 approaches in Java, Python, C++ and JavaScript, from linear scan up.
Problem
Given a sorted array and a value x, return the index of the ceil of x: the smallest element that is greater than or equal to x. If none exists, return -1.
Examples
Example 1
- Input
arr = [1, 2, 8, 10, 10, 12, 19], x = 5- Output
2- Why
- The smallest value >= 5 is 8, at index 2.
Example 2
- Input
arr = [1, 2, 8], x = 20- Output
-1
Constraints
1 <= arr.length <= 10^5, sorted non-decreasing.- With duplicates, return the first index of the ceil value.
Solve it here
Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.
Your own testcases: Run checks these, and the expected output comes from our reference solution.
My submissions
Nothing yet. Every Run and Submit you make while signed in shows up here.
| When | Result | Tests | Runtime | Your time | Type |
|---|
Run checks the testcases above (2 examples, or your own). Submit also runs 12 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.
Animated walkthrough
A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.
Concepts first, then the problem and every approach, step by step.
Space to play or pause · ← → to jump a step · click or drag the bar to seek
Solutions
Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the code in Java, Python, C++ and JavaScript.
| Approach | Time | Space |
|---|---|---|
| Linear scan | O(n) | O(1) |
| Optimal (bisect_left) | O(log n) | O(1) |
1Linear scan
O(n)O(1)Return the first index whose value is at least x.
- Walk until arr[i] >= x.
class Solution {
public int findCeil(int[] arr, int x) {
for (int i = 0; i < arr.length; i++) if (arr[i] >= x) return i;
return -1;
}
}class Solution:
def findCeil(self, arr: List[int], x: int) -> int:
for i, v in enumerate(arr):
if v >= x:
return i
return -1class Solution {
public:
int findCeil(vector<int>& arr, int x) {
for (int i = 0; i < (int)arr.size(); i++) if (arr[i] >= x) return i;
return -1;
}
};function findCeil(arr, x) {
for (let i = 0; i < arr.length; i++) if (arr[i] >= x) return i;
return -1;
}2Optimal (bisect_left)
O(log n)O(1)The first index with a value >= x is the ceil. If it equals n, nothing qualifies.
- lo = bisect_left(arr, x).
- Return lo < n ? lo : -1.
class Solution {
public int findCeil(int[] arr, int x) {
int lo = 0, hi = arr.length;
while (lo < hi) {
int mid = (lo + hi) >>> 1;
if (arr[mid] >= x) hi = mid; else lo = mid + 1;
}
return lo < arr.length ? lo : -1;
}
}class Solution:
def findCeil(self, arr: List[int], x: int) -> int:
lo, hi = 0, len(arr)
while lo < hi:
mid = (lo + hi) // 2
if arr[mid] >= x:
hi = mid
else:
lo = mid + 1
return lo if lo < len(arr) else -1class Solution {
public:
int findCeil(vector<int>& arr, int x) {
int lo = 0, hi = arr.size();
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (arr[mid] >= x) hi = mid; else lo = mid + 1;
}
return lo < (int)arr.size() ? lo : -1;
}
};function findCeil(arr, x) {
let lo = 0, hi = arr.length;
while (lo < hi) {
const mid = (lo + hi) >>> 1;
if (arr[mid] >= x) hi = mid; else lo = mid + 1;
}
return lo < arr.length ? lo : -1;
}Edge cases to test
- x larger than every element (-1)
- x equal to an element
Hints
Hint 1
The ceil is at bisect_left(x), if that index is inside the array.
FAQ
What is the best time complexity for Ceil in a Sorted Array?
Optimal (bisect_left) runs in O(log n) time and O(1) extra space.
Which pattern does Ceil in a Sorted Array use?
It is a binary search problem that uses the bisect pattern. Other problems with the same pattern: bisect_left, bisect_right, Search Insert Position, Floor in a Sorted Array.
Is there a brute force solution for Ceil in a Sorted Array?
Yes. Linear scan takes O(n) time and O(1) space. Return the first index whose value is at least x.
Which edge cases should I test for Ceil in a Sorted Array?
x larger than every element (-1); x equal to an element.