Search⌘ K
AI Features

Solution: First Missing Positive

Explore how to solve the first missing positive integer problem by applying cyclic sort. Learn to place elements in their correct positions in a single pass, enabling efficient in-place sorting that runs in linear time and constant space. Understand this approach through examples and gain confidence to implement it for coding interviews.

Statement

Given an unsorted integer array, nums, return the smallest missing positive integer. Create an algorithm that runs with an O(n)O(n) time complexity and utilizes a constant amount of space.

Note: The smallest missing positive isn’t the first positive number that’s missing in the range of elements in the input, but the first positive number that’s missing if we start from 11.

Constraints:

  • 11 \leq nums.length 105\leq 10^5 ...