Search⌘ K
AI Features

Solution: First Missing Positive

Explore an efficient approach to identify the smallest missing positive integer in an unsorted array. This lesson guides you through applying cyclic sort to rearrange elements in place, ensuring O(n) time complexity and constant space usage. You will understand how to detect misplaced elements after sorting to determine the missing positive value, optimizing problem-solving skills 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 ...