First Missing Positive

Smallest missing positive integer — cyclic sort placing each value at index value-1.

Hard
Time O(n) Space O(1)
LeetCode
3 min read
cyclic-sort

Problem#

Return the smallest positive integer missing from nums. O(n) time, O(1) extra space.

Examples#

  • [1,2,0]3
  • [3,4,-1,1]2
  • [7,8,9,11,12]1

Constraints#

  • 1 <= n <= 10^5.

Approach#

Cyclic sort: for each i, swap nums[i] to its “home” position nums[i] - 1 if it’s in [1, n] and not already there. Then scan for the first i where nums[i] != i + 1.

Solution#

First Missing Positive
class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
int n = nums.size();
for (int i = 0; i < n; ) {
if (nums[i] >= 1 && nums[i] <= n && nums[nums[i] - 1] != nums[i]) {
swap(nums[i], nums[nums[i] - 1]);
} else {
++i;
}
}
for (int i = 0; i < n; ++i) if (nums[i] != i + 1) return i + 1;
return n + 1;
}
};

Editorial#

The answer is always in [1, n+1]. Cyclic sort places every value in [1, n] at index value - 1. Anything else (out-of-range, duplicate) stays — those indices remain mismatched, and the first one is the answer. The nums[nums[i] - 1] != nums[i] guard prevents infinite swap loops on duplicates.

Complexity#

  • Time: O(n) amortized.
  • Space: O(1).

Concept revision#

Search ESC

Keyboard shortcuts

Shortcuts are disabled while typing in inputs.