Floor in a Sorted Array (original) (raw)
Last Updated : 16 Feb, 2026
Given a sorted array and a value **x, find the element of the floor of x. The floor of x is the largest element in the array smaller than or equal to x.
**Examples:
**Input: arr[] = [1, 2, 8, 10, 10, 12, 19], x = 5
**Output: 1
**Explanation: Largest number less than or equal to 5 is 2, whose index is 1**Input: arr[] = [1, 2, 8, 10, 10, 12, 19], x = 20
**Output: 6
**Explanation: Largest number less than or equal to 20 is 19, whose index is 6**Input : arr[] = [1, 2, 8, 10, 10, 12, 19], x = 0
**Output : -1
**Explanation: Since floor doesn't exist, output is -1.
Table of Content
- [Naive Method] – Linear Search – O(n) Time and O(1) Space
- [Expected Approach] – Binary Search – O(Log n) Time and O(1) Space
- [Using Library Methods] - O(Log n) Time and O(1) Space
[Naive Method] – Using Linear Search – O(n) Time and O(1) Space
Traverse the array from left to right while keeping track of the last element that is less than or equal to x. Since the array is sorted, the moment an element exceeds x, the previously seen valid element is the floor. If the traversal completes without exceeding x, the last element is the floor.
C++ `
#include #include using namespace std;
int findFloor(vector &arr, int x){ int n = arr.size(); // If last element is smaller than x if (x >= arr[n - 1]) return n - 1;
// If first element is greater than x
if (x < arr[0])
return -1;
// Linearly search for the first element greater than x
int ans = -1;
for (int i = 0; i < n; i++) {
if (arr[i] > x) {
return (i - 1);
}
}
return ans;}
int main() { vector arr = {1, 2, 4, 6, 10, 12, 14}; int x = 7; int index = findFloor(arr, x); cout << index; return 0; }
Java
import java.util.Arrays;
class GfG {
static int findFloor(int arr[], int x){
int n = arr.length;
// If last element is smaller than x
if (x >= arr[n - 1])
return n - 1;
// If first element is greater than x
if (x < arr[0])
return -1;
// Linearly search for the first element greater than x
int ans = -1;
for (int i = 0; i < n; i++) {
if (arr[i] > x) {
return (i-1);
}
}
return ans;
}
public static void main(String[] args){
int arr[] = {1, 2, 4, 6, 10, 12, 14};
int x = 7;
int index = findFloor(arr, x);
System.out.print(index);
}}
Python
def findFloor(arr, n, x): # If last element is smaller than x if (x >= arr[n - 1]): return n - 1
# If first element is greater than x
if (x < arr[0]):
return -1
# Linearly search for the first element
ans = -1
for i in range(0, n):
if (arr[i] > x):
return (i-1)
return ansif name == "main": arr = [1, 2, 4, 6, 10, 12, 14] n = len(arr) x = 7 index = findFloor(arr, n - 1, x)
print(index)C#
using System;
class GfG {
static int findFloor(int[] arr, int x){
// If last element is smaller than x
int n = arr.Length;
if (x >= arr[n - 1])
return n - 1;
// If first element is greater than x
if (x < arr[0])
return -1;
// Linearly search for the first element greater than x
int ans = -1;
for (int i = 0; i < n; i++)
if (arr[i] > x) {
return (i-1);
}
return ans;
}
static void Main()
{
int[] arr = {1, 2, 4, 6, 10, 12, 14};
int x = 7;
int index = findFloor(arr, x);
Console.WriteLine(index);
}}
JavaScript
function findFloor(arr, x) { let n = arr.length; if (x >= arr[n - 1]) return n - 1;
// If first element is greater than x
if (x < arr[0])
return -1;
// Linearly search for the first element greater than x
let ans = -1;
for (let i = 0; i < n; i++)
if (arr[i] > x) {
return (i-1);
}
return ans;}
// Driver Code let arr = [1, 2, 4, 6, 10, 12, 14]; let n = arr.length; let x = 7; let index = findFloor(arr, x); console.log(index);
`
[Expected Approach] – Using Binary Search – O(Log n) Time and O(1) Space
Find the midpoint. If
arr[mid]equalsx, returnmid. Ifarr[mid]is greater thanx, search the left half. Otherwise, storearr[mid]as a potential floor and continue searching the right half for a larger but valid floor value.
C++ `
#include #include using namespace std;
int findFloor(vector &arr, int x){ int ans = -1; int low = 0; int high = arr.size() - 1; while (low <= high){ int mid = (low + high) / 2; if (arr[mid] > x){ high = mid - 1; } else if (arr[mid] <= x){ ans = mid; low = mid + 1; } } return ans; }
int main(){ vector arr = {1, 2, 4, 6, 10, 12, 14}; int x = 7;
int index = findFloor(arr, x);
cout << index;
return 0;}
Java
import java.io.*;
class GfG {
static int findFloor(int arr[], int x){
int n = arr.length;
int low = 0, high = n - 1;
int result = -1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] <= x) {
result = mid;
low = mid + 1;
}
else {
high = mid - 1; // Move left
}
}
return result;
}
public static void main(String[] args){
int arr[] = {1, 2, 4, 6, 10, 12, 14};
int x = 7;
// Function call
int index = findFloor(arr, x);
System.out.println(index);
}}
Python
def findFloor(arr, x):
n = len(arr)
low, high = 0, n - 1
result = -1
while low <= high:
mid = (low + high) // 2
if arr[mid] <= x:
result = mid
low = mid + 1
else:
high = mid - 1
return resultif name == "main": arr = [1, 2, 4, 6, 10, 12, 14] x = 7
index = findFloor(arr, x)
print(index)C#
using System;
class GfG {
static int findFloor(int[] arr, int x){
int n = arr.Length;
int low = 0, high = n - 1;
int result = -1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] <= x) {
result = mid;
low = mid + 1;
}
else {
high = mid - 1;
}
}
return result;
}
public static void Main(){
int[] arr = { 1, 2, 4, 6, 10, 12, 14 };
int x = 7;
int index = findFloor(arr, x);
Console.Write(index);
}}
JavaScript
function findFloor(arr, x){
let n = arr.length;
let low = 0, high = n - 1;
let result = -1;
while (low <= high) {
let mid = Math.floor((low + high) / 2);
if (arr[mid] <= x) {
result = mid;
low = mid + 1;
}
else {
high = mid - 1;
}
}
return result;}
// Driver code let arr = [ 1, 2, 4, 6, 10, 12, 14 ]; let x = 7; let index = findFloor(arr, x); console.log(index);
`
[Alternate Approach] Using Library Methods - O(Log n) Time and O(1) Space
We can use the following library methods in different languages.
The upper_bound() method in C++, Arrays.binarySearch() in Java and C#, bisect_right() in Python and findIndex
C++ `
#include #include #include using namespace std;
int findFloor(vector& arr, int x) { auto itr = upper_bound(arr.begin(), arr.end(), x); if (itr == arr.begin()) return -1; itr--; return itr - arr.begin(); }
int main() { vector arr = { 1, 2, 4, 6, 10, 12, 14 }; int x = 7; int index = findFloor(arr, x);
cout<<index;
return 0;}
Java
import java.util.Arrays;
class GfG { static int findFloor(int[] arr, int x) { int idx = Arrays.binarySearch(arr, x);
// If element not found, get insertion position
if (idx < 0) {
// Move to the previous index
idx = Math.abs(idx) - 2;
}
return (idx >= 0) ? idx : -1;
}
public static void main(String[] args) {
int arr[] = { 1, 2, 4, 6, 10, 12, 14 };
int x = 7;
int floorindex = findFloor(arr, x);
System.out.println(floorindex);
}}
Python
import bisect
def findFloor(arr, x):
idx = bisect.bisect_right(arr, x) - 1
return idx if idx >= 0 else -1
arr = [1, 2, 4, 6, 10, 12, 14] x = 7 index = findFloor(arr, x)
print(index)
C#
using System;
class GfG { static int findFloor(int[] arr, int x) { int idx = Array.BinarySearch(arr, x);
// If x is found in the array, return the index directly
if (idx >= 0) {
return idx;
}
// If x is not found, find the largest element smaller than x
// ~idx = -(insertion_point) - 1
idx = ~idx - 1;
// If idx is valid, return it; otherwise, return -1 (no floor exists)
return (idx >= 0) ? idx : -1;
}
public static void Main() {
int[] arr = { 1, 2, 4, 6, 10, 12, 14 };
int x = 7;
int index = findFloor(arr, x);
Console.WriteLine(index);
}}
JavaScript
function findFloor(arr, x) {
// Use findLastIndex() to find the last element that is <= x
let idx = arr.slice().reverse().findIndex(val => val <= x);
// Adjust index since findLastIndex() is used on a reversed array
if (idx !== -1) {
idx = arr.length - 1 - idx;
}
return idx;}
let arr = [ 1, 2, 4, 6, 10, 12, 14 ]; const x = 7; let index = findFloor(arr, x);
console.log(index)
`
**Related Articles: