# Solution: Find the Floor and Ceil of a Number in a Sorted Array

Review various approaches in detail to find the floor and ceiling values of a given number from a sorted array.

## Solution 1

The naive approach to this problem would be:

- Traverse the array starting from the 0th
`index`

to the last`index`

. - Compare value at each index
`arr[i]`

with the given input`integer`

$x$. - When you reach the maximum value lesser than equal to $x$, store it as the $floor$ value.
- Similarly, the minimum value greater than equal to $x$ will be stored as $ceiling$ value while you linearly traverse the array.
- Terminate the traversal when you reach the end of the array.

### Time complexity

Since the loop iterates every single element at least once, the running complexity for this solution would be $O(n)$, where $n$ is the size of the array.

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.