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



void ceilandfloorBinary(vector<int> &arr, int k)
{

    // find the closest greater than number & closet lesser number
    int _ceil = INT_MAX;
    int _floor = INT_MIN;

    sort(arr.begin(), arr.end());

    int l = 0, r = arr.size() - 1;

    while (l <= r)
    {
        int mid = (r - l) / 2 + l;
        if (arr[mid] == k)
        {
            _ceil = k;
            _floor = k;
            break;
        }
        else if (arr[mid] > k)
        {
            _ceil = arr[mid];
            r = mid - 1;
        }
        else
        {
            _floor = arr[mid];
            l = mid + 1;
        }
    }

    if (_ceil != INT_MAX)
        cout << "ceiling of " << k << " is: " << _ceil << "\n";
    else
        cout << "There is no ceiling of " << k << "\n";

    if (_floor != INT_MIN)
        cout << "floor of " << k << " is: " << _floor << "\n";
    else
        cout << "There is no floor of " << k << "\n";
}

int main()
{
    cout << "Enter number of elements\n";
    int n;
    cin >> n;
    vector<int> arr(n);
    cout << "Enter the elements\n";

    for (int i = 0; i < n; i++)
        cin >> arr[i];

    cout << "Enter the number for which you need ceil & floor\n";
    int k;
    cin >> k;

    // using binary search
    ceilandfloorBinary(arr, k);

    return 0;
}

Output

Enter number of elements
5
Enter the elements
2
6
7
9
23
Enter the number for which you need ceil & floor
6
ceiling of 6 is: 6
floor of 6 is: 6