Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Finding Floor and Ceil of a Sorted Array using C++ STL

  • Last Updated : 26 Oct, 2021

Given a sorted array, the task is to find the floor and ceil of given numbers using STL.
Examples: 
 

Input: arr[] = {1, 2, 4, 7, 11, 12, 23, 30, 32},
       values[] = { 1, 3, 5, 7, 20, 24 }
Output: Floor Values: 1 2 4 7 12 23 
       Ceil values: 1 4 7 7 23 30 

In case of floor(): lower_bound() method os STL will be used to find the lower bound. This returns an iterator pointing to the first element in the range [first,last) which does not compare less than the target.
In case of ceil(): upper_bound() method os STL will be used to find the upper bound. This method returns an iterator pointing to the first element in the range [first,last) which compares greater than a target.
Implementation: 
 

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for the language and STL. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.

CPP




// C++ program to find the floor and ceil
// of a given numbers in a sorted array
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to find floor of list of
// values using lower_bound in STL
void printFloor(int arr[], int n1,
                int findFloor[], int n2)
{
    // Find and print the Floor Values
    int low;
 
    cout << "Floor : ";
    for (int i = 0; i < n2; i++) {
 
        low = (lower_bound(arr, arr + n1,
                           findFloor[i])
               - arr);
 
        if (arr[low] > findFloor[i])
            cout << arr[low - 1] << " ";
        else
            cout << arr[low] << " ";
    }
 
    cout << endl;
}
ceil
// Function to find Ceil of list of
// values using upper_bound in STL
void printCeil(int arr[], int n1,
               int findCeil[], int n2)
{
    // Find and print the Ceil Values
    int up;
    cout << "Ceil : ";
    for (int i = 0; i < n2; i++) {
 
        up = (upper_bound(arr, arr + n1,
                          findCeil[i])
              - arr);
 
        if (arr[up] > findCeil[i]
            && arr[up - 1] == findCeil[i]) {
            cout << arr[up - 1] << " ";
        }
        else
            cout << arr[up] << " ";
    }
    cout << endl;
}
 
// Driver code
int main()
{
    // Get the sorted array
    int arr[] = { 1, 2, 4, 7, 11, 12, 23, 30, 32 };
    int n1 = sizeof(arr) / sizeof(arr[0]);
 
    // Print Array
    cout << "Original Array: ";
    for (unsigned int i = 0; i < n1; i++)
        cout << " " << arr[i];
    cout << "\n";
 
    // Given values whose floor and ceil
    // values are needed to find
    int find[] = { 1, 3, 5, 7, 20, 24 };
    int n2 = sizeof(find) / sizeof(find[0]);
 
    // Print Values whose floor
    // and ceil is to be found
    cout << "Values: ";
    for (unsigned int i = 0; i < n2; i++)
        cout << find[i] << " ";
    cout << "\n";
 
    // Print Floor Values
    printFloor(arr, n1, find, n2);
 
    // Print Ceil Values
    printCeil(arr, n1, find, n2);
 
    return 0;
}
Output: 
Array:  1 2 4 7 11 12 23 30 32
Values: 1 3 5 7 20 24 
Floor : 1 2 4 7 12 23 
Ceil  : 1 4 7 7 23 30

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!