First Missing Positive

Hard

Given an unsorted integer array nums, return the smallest positive integer that does not appear in it.

Your algorithm must run in O(n) time and use O(1) extra space.

Example 1

firstMissingPositive()
Input
nums = [1,2,0]
Output
3

Example 2

firstMissingPositive()
Input
nums = [3,4,-1,1]
Output
2
Constraints
1 <= nums.length <= 10^5
-2^31 <= nums[i] <= 2^31 - 1

Topics

ArraysHash Table

Implement firstMissingPositive in the editor and return the answer — printing it is never graded.