Floor and ceil in an unsorted array

Last Updated : 16 Apr, 2026

Given an unsorted array arr[] and a value x, find the floor and ceiling of x in the array. The floor is the largest number in the array that is less than or equal to x, while the ceiling is the smallest number in the array that is greater than or equal to x.

Note: If x is smaller than every number in the array, the floor doesn't exist. Similarly, if x is larger than every number, the ceiling doesn't exist. In either case, return -1 for the missing value.

Examples: 

Input: x = 7 , arr[] = [5, 6, 8, 9, 6, 5, 5, 6]
Output: 6, 8
Explanation: Floor of 7 is 6 and ceil of 7 is 8.

Input: x = 10 , arr[] = [5, 6, 8, 8, 6, 5, 5, 6]
Output: 8, -1
Explanation: Floor of 10 is 8 but ceil of 10 is not possible.

[Expected Approach] Using Linear Search - O(n) Time and O(1) Space

Traverse the array and keep track of two differences with respect to x. 

  1. Minimum difference of element greater than or equal to x. 
  2. Minimum difference of element smaller than or equal to x.
C++
#include <iostream>
#include <vector>
#include <climits>

using namespace std;

// Function to floor and ceiling of x in arr[] 
vector<int> getFloorAndCeil(int x, vector<int> arr) {
    int n = arr.size();
    int floorDiff = INT_MAX, ceilDiff = INT_MAX;
    int ceilVal = -1, floorVal = -1;

    // Looping through the array to find the floor and ceil of x
    for (int i = 0; i < n; i++) {
        
        // Updating the ceil if the current element is 
        // greater than or equal to x and the difference
        // is smaller
        if (arr[i] >= x && ceilDiff > (arr[i] - x)) {
            ceilDiff = arr[i] - x;
            ceilVal = arr[i];
        }
        
        // Updating the floor if the current element is 
        // smaller than or equal to x and the difference
        // is smaller
        if (arr[i] <= x && floorDiff > (x - arr[i])) {
            floorDiff = x - arr[i];
            floorVal = arr[i];
        }
    }

    return {floorVal, ceilVal};
}

int main() {
    vector<int> arr = {5, 6, 8, 9, 6, 5, 5, 6};
    int x = 7;
    vector<int> ans = getFloorAndCeil(x, arr); 
    int floor_of_x = ans[0];
    int ceil_of_x = ans[1];
    
    cout << floor_of_x << " " << ceil_of_x << "\n";
    
    return 0;
}
Java
import java.util.Vector;

class GFG {
    // Function to floor and ceiling of x in arr[]
    public static int[] getFloorAndCeil(int x, int[] arr) {
        int n = arr.length;
        int floorDiff = Integer.MAX_VALUE;
        int ceilDiff = Integer.MAX_VALUE;
        int ceilVal = -1;
        int floorVal = -1;

        // Looping through the array to find the floor and ceil of x
        for (int i = 0; i < n; i++) {
            
            // Updating the ceil if the current element is greater than or equal to x
            // and the difference is smaller
            if (arr[i] >= x && ceilDiff > (arr[i] - x)) {
                ceilDiff = arr[i] - x;
                ceilVal = arr[i];
            }
            
            // Updating the floor if the current element is smaller than or equal to x
            // and the difference is smaller
            if (arr[i] <= x && floorDiff > (x - arr[i])) {
                floorDiff = x - arr[i];
                floorVal = arr[i];
            }
        }

        return new int[]{floorVal, ceilVal};
    }

    public static void main(String[] args) {
        int[] arr = {5, 6, 8, 9, 6, 5, 5, 6};
        int x = 7;

        int[] ans = getFloorAndCeil(x, arr);
        int floor_of_x = ans[0];
        int ceil_of_x = ans[1];

        System.out.println(floor_of_x + " " + ceil_of_x);
    }
}
Python
import sys

# Function to floor and ceiling of x in arr[]
def getFloorAndCeil(x, arr):
    n = len(arr)
    floorDiff = sys.maxsize
    ceilDiff = sys.maxsize
    ceilVal = -1
    floorVal = -1

    # Looping through the array to find the floor and ceil of x
    for i in range(n):
        
        # Updating the ceil if the current element is greater than or equal to x
        # and the difference is smaller
        if arr[i] >= x and ceilDiff > (arr[i] - x):
            ceilDiff = arr[i] - x
            ceilVal = arr[i]
        
        # Updating the floor if the current element is smaller than or equal to x
        # and the difference is smaller
        if arr[i] <= x and floorDiff > (x - arr[i]):
            floorDiff = x - arr[i]
            floorVal = arr[i]

    return [floorVal, ceilVal]

if __name__ == "__main__":
    arr = [5, 6, 8, 9, 6, 5, 5, 6]
    x = 7
    
    ans = getFloorAndCeil(x, arr)
    print(f"{ans[0]} {ans[1]}")
C#
using System;

class GFG {
    // Function to floor and ceiling of x in arr[]
    public static int[] GetFloorAndCeil(int x, int[] arr) {
        int n = arr.Length;
        int floorDiff = int.MaxValue;
        int ceilDiff = int.MaxValue;
        int ceilVal = -1;
        int floorVal = -1;

        // Looping through the array to find the floor and ceil of x
        for (int i = 0; i < n; i++) {
            
            // Updating the ceil if the current element is greater than or equal to x
            // and the difference is smaller
            if (arr[i] >= x && ceilDiff > (arr[i] - x)) {
                ceilDiff = arr[i] - x;
                ceilVal = arr[i];
            }
            
            // Updating the floor if the current element is smaller than or equal to x
            // and the difference is smaller
            if (arr[i] <= x && floorDiff > (x - arr[i])) {
                floorDiff = x - arr[i];
                floorVal = arr[i];
            }
        }

        return new int[] { floorVal, ceilVal };
    }

    static void Main() {
        int[] arr = { 5, 6, 8, 9, 6, 5, 5, 6 };
        int x = 7;

        int[] ans = GetFloorAndCeil(x, arr);
        Console.WriteLine(ans[0] + " " + ans[1]);
    }
}
JavaScript
// Function to floor and ceiling of x in arr[]
function getFloorAndCeil(x, arr) {
    let n = arr.length;
    let floorDiff = Number.MAX_SAFE_INTEGER;
    let ceilDiff = Number.MAX_SAFE_INTEGER;
    let ceilVal = -1;
    let floorVal = -1;

    // Looping through the array to find the floor and ceil of x
    for (let i = 0; i < n; i++) {
        
        // Updating the ceil if the current element is greater
        // than or equal to x and the difference is smaller
        if (arr[i] >= x && ceilDiff > (arr[i] - x)) {
            ceilDiff = arr[i] - x;
            ceilVal = arr[i];
        }
        
        // Updating the floor if the current element is
        // smaller than or equal to x and the difference 
        // is smaller
        if (arr[i] <= x && floorDiff > (x - arr[i])) {
            floorDiff = x - arr[i];
            floorVal = arr[i];
        }
    }

    return [floorVal, ceilVal];
}

// Driver code
const arr = [5, 6, 8, 9, 6, 5, 5, 6];
const x = 7;

const ans = getFloorAndCeil(x, arr);
console.log(ans[0] + " " + ans[1]);

Output
6 8

Related Articles : 
Ceiling in a sorted array 
Floor in a sorted array


Comment