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
Inputnums = [1,2,0]
Output3
The numbers 1 and 2 are present, so the smallest missing positive integer is 3.
Example 2
Inputnums = [3,4,-1,1]
Output2
The 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

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate