216. First Missing Positive

Hard
Cyclic Placement
Array

Problem

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

Examples

Example 1

Input: [1,2,0]
Output: 3

Example 2

Input: [3,4,-1,1]
Output: 2

Example 3

Input: [7,8,9,11,12]
Output: 1
Constraints

1 <= nums.length <= 100000

-2147483648 <= nums[i] <= 2147483647

Hints

?? The answer is always between 1 and n + 1.

?? Try placing each value x at index x - 1 whenever possible.

Expected Complexity
Time: O(n)
Space: O(1)
Follow-up

Can you solve it in O(n) time and O(1) extra space?

Practice Notes

Attempts: 0

Time spent: 0min

Hints used: 0

00:00
Loading...
Case 1
Case 2
Case 3
Input
[1,2,0]
Expected
3