dsa · hard

First Missing Positive

Given an unsorted integer array nums, return the smallest missing positive integer. Positives start at 1. Zeros and negatives do not count. The answer is at most len(nums) + 1.

Arguments

Example

[1,2,0] contains 1 and 2, so the smallest missing positive is 3.

[3,4,-1,1] contains the positives 1, 3, 4. 2 is absent → 2.

[7,8,9,11,12] contains none of 1..5, so the answer is 1.

Constraints

1 <= nums.length <= 4*10^4 -2^31 <= nums[i] <= 2^31 - 1 Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.

Examples

Example 1

Input:
[1,2,0]

Expected:
3

Example 2

Input:
[3,4,-1,1]

Expected:
2

Example 3

Input:
[7,8,9,11,12]

Expected:
1

Open in the Dojo editor