Open In App

Floor in a Sorted Array

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
42 Likes
Like
Report

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.

[Naive Method] – Using Linear Search – O(n) Time and O(1) Space

Traverse the array from start to end. If the first element is greater than x, print "Floor of x doesn't exist." Otherwise, when encountering an element greater than x, print the previous element and exit. If no such element is found, print the last element as the floor of x.

C++
#include <bits/stdc++.h>
using namespace std;

/* An function to get
index of floor of x in arr */
int findFloor(vector<int> &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;
}

/* Driver program to check above functions */
int main()
{
    vector<int> arr = {1, 2, 4, 6, 10, 12, 14};
    int x = 7;
    int index = findFloor(arr, x);
    cout << index;
    return 0;
}
Java Python C# JavaScript

Output
3

[Expected Approach] – Using Binary Search – O(Log n) Time and O(1) Space

Find the midpoint. If arr[mid] equals x, return mid. If arr[mid] is greater than x, search the left half. Otherwise, store arr[mid] as a potential floor and continue searching the right half for a larger but valid floor value.

C++
#include <bits/stdc++.h>
using namespace std;

int findFloor(vector<int> &arr, int x)
{
    // Your code here
    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<int> arr = {1, 2, 4, 6, 10, 10, 12, 14};
    int x = 11;

    // Function call
    int index = findFloor(arr, x);
    cout << index;
    return 0;
}
Java Python C# JavaScript

Output
3

[Another 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 <bits/stdc++.h>
using namespace std;

int findFloor(vector<int>& 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<int> arr = { 1, 2, 4, 6, 10, 12, 14 };
    int x = 7;
    int index = findFloor(arr, x);
    
    cout<<index;
    return 0;
}
Java Python C# JavaScript

Output
3

Related Articles: 


Similar Reads