Floor And Ceiling In An Array
November 21, 2017
We looked at variants of binary search in two recent exercises. Today we look at a third variant.
Your task is to write a variant of binary search in a sorted array without duplicates that returns the index of the two elements immediately below and above a target; if the target is in the array, both return values should point to the target value. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
The design is based on the predicates in the comments.
Here’s a solution in C99.
Build and run:
Output: