First Missing Positive
Given an unsorted integer array nums, return the smallest positive integer that is not present in nums.
You must implement an algorithm that runs in O(n) time and uses O(1) auxiliary space.
Example 1
Input
nums = [1,2,0]Output
3The numbers 1 and 2 are present, so the smallest missing positive integer is 3.
Example 2
Input
nums = [3,4,-1,1]Output
2The number 1 is present, but 2 is missing, making it the smallest missing positive integer.
Constraints
- 1 <= nums.length <= 10^5
- -2^31 <= nums[i] <= 2^31 - 1
Asked at 35 companies
Practice questions like this out loud in a mock coding interview with a live AI interviewer. First one free.