Back to DSA

First Missing Positive

hard
Acceptance: 38%
Arrays

Given an unsorted array of integers, find the smallest positive integer that does not appear in the array. Your solution must achieve linear time complexity and use only constant additional space beyond the input array itself.

Examples

Example 1:
Input:nums = [5,3,1]
Output:2
Explanation: The positive integers present are 1, 3, 5. The smallest absent positive integer is 2.
Example 2:
Input:nums = [1,2,3,4]
Output:5
Explanation: All integers from 1 through 4 are present, so the answer is 5.

Hints

00:00
1234567