Ceiling Value in a Sorted Array
Updated
Problem statement
Given an array nums sorted from smallest to largest and an integer key, return key if it is present. Otherwise, return the smallest array value greater than key. Return -1 when no such value exists.
Return the value, rather than its index. The answer need not be key + 1. For this exercise, assume array values and the key are nonnegative.
Function
ceilingValue(nums: int[], key: int) → intExamples
Example 1
nums = [2,5,5,11]key = 6return = 11The smallest value greater than 6 is 11, even though it is not 7.
Example 2
nums = [2,5,5,11]key = 5return = 5The exact key occurs, so return 5.
Constraints
- For this exercise, assume
0 <= nums.length <= 10^5. 0 <= nums[i], key <= 10^9.- The array is sorted in nondecreasing order; duplicate values are allowed.