Open In App

Ceiling in a sorted array

Last Updated : 28 Feb, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Given a sorted array and a value x, find index of the ceiling of x. The ceiling of x is the smallest element in an array greater than or equal to x.

Note: In case of multiple occurrences of ceiling of x, return the index of the first occurrence.

Examples : 

Input: arr[] = [1, 2, 8, 10, 10, 12, 19], x = 5
Output: 2
Explanation: Smallest number greater than 5 is 8, whose index is 2.

Input: arr[] = [1, 2, 8, 10, 10, 12, 19], x = 20
Output: -1
Explanation: No element greater than 20 is found. So output is -1.

Input: arr[] = [1, 1, 2, 8, 10, 10, 12, 19], x = 0
Output: 0
Explanation: Smallest number greater than 0 is 1, whose indices are 0 and 1. The index of the first occurrence is 0.

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

  1. If x is smaller than or equal to the first element in the array then return 0(index of the first element).
  2. Else linearly search for an index i such that x lies between arr[i] and arr[i+1]. 
  3. If we do not find an index i in step 2, then return -1. 
C++
#include <bits/stdc++.h>
using namespace std;

/* Function to get index of ceiling of x in arr */
int ceilSearch(vector<int>& arr, int x) 
{   
    /* If x is smaller than or equal to first element, 
        then return the first element */
    if(x <= arr[0]) 
        return 0; 
  
    /* Otherwise, linearly search for ceil value */
    for(int i = 0; i < arr.size() - 1; i++) 
    { 
        if(arr[i] == x) 
            return i; 
      
        /* if x lies between arr[i] and arr[i+1] including 
        arr[i+1], then return arr[i+1] */
        if(arr[i] < x && arr[i+1] >= x) 
            return i+1; 
    }     
    /* If we reach here then x is greater than the last element 
        of the array, return -1 in this case */
    return -1; 
} 

/* Driver code*/
int main() 
{ 
    vector<int> arr = {1, 2, 8, 10, 10, 12, 19}; 
    int x = 3; 
    int index = ceilSearch(arr, x); 
    if(index == -1) 
        cout << "Ceiling of " << x << " doesn't exist in array "; 
    else
        cout << "ceiling of " << x << " is " << arr[index]; 
    return 0; 
}
C Java Python C# JavaScript

Output
ceiling of 3 is 8

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

Since the input array is sorted, we can use binary search.

  • Find the mid point.
  • Else If arr[mid] < x, go to the right side
  • Else (arr[mid] >= x), we found a potential candidate for ceiling. Go to the left side and try finding a smaller value which is greater than or equal to x.

Let us understand the approach with an example

arr = [1, 2, 8, 10, 10, 12, 19] , x = 3

Initial: lo = 0, hi = 6, mid = 3 → arr[3] = 10 (res = 10, Move Left)

Step 2: lo = 0, hi = 2, mid = 1 → arr[1] = 2 (Move Right)

Step 3: lo = 2, hi = 2, mid = 2 → arr[2] = 8 (res = 8, Move Left)

Final: lo > hi, return res = 2

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

/* Function to find the ceiling of x using
   binary search */
int ceilSearch(vector<int>& arr, int x) {
    int lo = 0, hi = arr.size() - 1, res = -1;
    
    while (lo <= hi) {
        int mid = lo + (hi - lo) / 2;
  
        if (arr[mid] < x)
            lo = mid + 1;      
        
        else { // Potential ceiling found
            res = mid;   
            hi = mid - 1;
        }
    }
    return res;  
}

int main() {
    vector<int> arr = {1, 2, 8, 10, 10, 12, 19};
    int x = 3;
    int index = ceilSearch(arr, x);
    
    if (index == -1) 
        cout << "Ceiling of " << x << " doesn't exist in array";
    else
        cout << "Ceiling of " << x << " is " << arr[index];

    return 0;
}
C Java Python C# JavaScript

Output
Ceiling of 3 is 8

Using Library Methods

We can use the following library methods in different languages.

The lower_bound() method in C++, Arrays.binarySearch() in Java and bisect_left() in Python

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

int main()
{
    vector<int> arr = { 1, 2, 8, 10, 10, 12, 19 };
    int n = arr.size();
    int x = 8;
    auto itr = lower_bound(arr.begin(), arr.end(),x); // returns iterator
    int idx = itr - arr.begin(); // converting to index;
    if (idx == n) {
        cout << "Ceil Element does not exist " << endl;
    }
    else {
        cout << "Ceil Element of " << x << " is " << arr[idx] << endl;
    }
    return 0;
}
Java Python C# JavaScript

Output
Ceil Element of 8 is 8

Related Articles: 
Floor in a Sorted Array 
Find floor and ceil in an unsorted array=



Next Article

Similar Reads

three90RightbarBannerImg