Floor in a Sorted Array
Floor in a Sorted Array is a easy binary search problem solved with the bisect pattern.
The best approach, optimal (bisect_right - 1), 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 floor of x: the largest element that is less 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
1- Why
- The largest value <= 5 is 2, at index 1.
Example 2
- Input
arr = [1, 2, 8], x = 0- Output
-1
Constraints
1 <= arr.length <= 10^6, sorted non-decreasing.- With duplicates, return the last index of the floor 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_right - 1) | O(log n) | O(1) |
1Linear scan
O(n)O(1)Keep the last index whose value is at most x.
- Walk while arr[i] <= x; answer i - 1.
class Solution {
public int findFloor(int[] arr, int x) {
int i = 0;
while (i < arr.length && arr[i] <= x) i++;
return i - 1;
}
}class Solution:
def findFloor(self, arr: List[int], x: int) -> int:
i = 0
while i < len(arr) and arr[i] <= x:
i += 1
return i - 1class Solution {
public:
int findFloor(vector<int>& arr, int x) {
int i = 0;
while (i < (int)arr.size() && arr[i] <= x) i++;
return i - 1;
}
};function findFloor(arr, x) {
let i = 0;
while (i < arr.length && arr[i] <= x) i++;
return i - 1;
}2Optimal (bisect_right - 1)
O(log n)O(1)bisect_right(x) is the first index with a value greater than x, so the index just before it holds the largest value <= x, or -1 if there is none.
- Find the first index with arr[i] > x.
- Return that index - 1.
class Solution {
public int findFloor(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 - 1;
}
}class Solution:
def findFloor(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 - 1class Solution {
public:
int findFloor(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 - 1;
}
};function findFloor(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 - 1;
}Edge cases to test
- x smaller than every element (-1)
- x equal to a repeated value (last occurrence)
Hints
Hint 1
The floor is at bisect_right(x) - 1.
FAQ
What is the best time complexity for Floor in a Sorted Array?
Optimal (bisect_right - 1) runs in O(log n) time and O(1) extra space.
Which pattern does Floor 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, Ceil in a Sorted Array.
Is there a brute force solution for Floor in a Sorted Array?
Yes. Linear scan takes O(n) time and O(1) space. Keep the last index whose value is at most x.
Which edge cases should I test for Floor in a Sorted Array?
x smaller than every element (-1); x equal to a repeated value (last occurrence).