469. Find Smallest Missing Positive in Sorted Array

EasyBinary SearchArrayBinary Search

Given a sorted array nums (may contain negatives, zeros, or duplicates), return the smallest positive integer that does not appear in the array. Solve in O(log n).

Input: A sorted integer array nums.

Output: Smallest missing positive integer (>= 1).

Examples

Example 1
Input: [1,2,3,4,5]
Output: 6
Explanation: All of 1-5 present; smallest missing positive is 6.
Example 2
Input: [-3,-2,-1,0,1,2,4]
Output: 3
Explanation: 1 and 2 present but 3 missing.
Example 3
Input: [2,3,4,5,6]
Output: 1
Explanation: 1 is the smallest missing positive.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →