Menu
CoddyTech

First Missing Positive

You get an unsorted array of integers nums. Return the smallest positive integer (1 or larger) that does not appear in nums. The array may hold negative numbers, zeros, repeated values and values far larger than its length.

Function

firstMissingPositive(nums: integer-array) → integer
numsinteger-array
the unsorted integers, which may repeat and may be negative, zero or very large
Returnsinteger
the smallest positive integer that is missing from nums

Constraints

  • 1 ≤ nums.length ≤ 104
  • -109 ≤ nums[i] ≤ 109
  • The values are in no particular order and may repeat.

Examples

Input
nums = [5, -2, 1, 3, 2, 9]
Output
4
Explanation
1, 2 and 3 are all in the array and 4 is not, so the answer is 4. The 5 and the 9 do not matter once a smaller value is missing.
challenge icon

Follow-up

Can you find the answer in O(n) time while using only O(1) extra memory?

Reset code
def firstMissingPositive(nums):
    # Write code here

Test #1

Test #2

Test #3

Argument

nums = [5, -2, 1, 3, 2, 9]

Expected Output

4