426. Find Ceil of an Element in Sorted Array

EasyBinary SearchArrayBinary Search

Given a sorted array nums of distinct integers and an integer target, return the smallest element in nums that is greater than or equal to target. Return -1 if no such element exists.

Input: A sorted distinct integer array nums and integer target.

Output: The ceiling value (smallest element >= target), or -1.

Examples

Example 1
Input: [1,3,5,7,9], 6
Output: 7
Explanation: Elements >= 6: {7,9}. Smallest is 7.
Example 2
Input: [1,3,5,7,9], 10
Output: -1
Explanation: No element >= 10 exists. Return -1.
Example 3
Input: [2,4,6,8,10], 6
Output: 6
Explanation: 6 is in array; ceil(6)=6.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →