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

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 method returns an iterator of the element greater than or equal to the specified element.

In case of ceil(): upper_bound() method os STL will be used to find the upper bound. This method returns an iterator of the element greater than or equal to the specified element.

Implementation:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}
  
// 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 ciel
    // 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;
}

chevron_right


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

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.