Problem 704
Floor and Ceil in a Sorted Array
Easy · 10 pts · Array, Binary Search
Problem
You are given an array sorted in non-decreasing order and a value X. The floor of X is the largest array element that is less than or equal to X, and the ceil of X is the smallest array element that is greater than or equal to X. Print both, using -1 for one that does not exist. Two binary searches (lower bound and upper bound) find them in O(log N).
Input
The first line contains N and X. The second line contains the N sorted values.
Output
Print the floor and the ceil of X separated by a space, using -1 when one does not exist.
Constraints
1 <= N <= 100000; 0 <= A[i], X <= 10^9; A is sorted
Example input
6 5 3 4 4 7 8 10
Example output
4 7
Explanation
In 3 4 4 7 8 10 with X = 5, the largest element not above 5 is 4 and the smallest element not below 5 is 7. So the answer is 4 7.