
Floor of 'x' is the largest element in the array which is smaller than or equal to 'x'.
Ceiling of 'x' is the smallest element in the array greater than or equal to 'x'.
Input:
n=6, x=5, a=[3, 4, 7, 8, 8, 10]
Output:
4
Explanation:
The floor and ceiling of 'x' = 5 are 4 and 7, respectively.
The first line contains two integers, ‘n’ and 'x', where n represents the size of the array.
The next line contains 'n' integers, denoting the elements of the given array.
Return two integers, the floor and ceiling of 'x'.
You are not required to print anything; it has already been handled. Just implement the function.
Simply iterate all the elements of the array. While iterating, check each number for floor and ceil.
The steps are as follows:
This question is similar to finding lower_bound of X, Hence we can use binary search to solve it on our own.
Start from a search space of the entire array, each time reduce the search space to half, continue doing this until the search space size becomes 0.
The steps are as follows: