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

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:  
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 STLvoid 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 STLvoid 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 codeint 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;}
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
 
jatishay444
akshaysingh98088
saurabh1990aror
Binary Search
STL
Technical Scripter 2018
C++
C++ Programs
Technical Scripter
STL
Binary Search
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Operator Overloading in C++
Polymorphism in C++
Sorting a vector in C++
Friend class and function in C++
Pair in C++ Standard Template Library (STL)
Header files in C/C++ and its uses
How to return multiple values from a function in C or C++?
C++ Program for QuickSort
Program to print ASCII Value of a character
Sorting a Map by value in C++ STL