🧠
Optimal Cyclic Sort / Index Placement
💡 This approach uses the idea that the smallest missing positive integer must be in the range 1 to n+1. By placing each number in its correct index (value - 1), we can find the first index where the number is incorrect, revealing the missing positive.
Intuition
Place each positive number x at index x-1 if possible. Then the first index i where nums[i] != i+1 is the missing positive integer.
Algorithm
- Iterate through the array.
- For each number nums[i], while it is in the range [1, n] and not at its correct position, swap it with the number at its target position nums[nums[i]-1].
- After rearrangement, iterate again to find the first index i where nums[i] != i+1.
- Return i+1 as the smallest missing positive integer.
💡 The key is to place numbers in their correct indices to detect the missing number by the first mismatch.
def firstMissingPositive(nums):
n = len(nums)
for i in range(n):
while 1 <= nums[i] <= n and nums[nums[i] - 1] != nums[i]:
nums[nums[i] - 1], nums[i] = nums[i], nums[nums[i] - 1]
for i in range(n):
if nums[i] != i + 1:
return i + 1
return n + 1
# Driver code
if __name__ == '__main__':
print(firstMissingPositive([3,4,-1,1])) # Output: 2
print(firstMissingPositive([1,2,0])) # Output: 3
Line Notes
n = len(nums)Store length for boundary checks to avoid index errors
for i in range(n)Iterate over each element to place it in the correct position
while 1 <= nums[i] <= n and nums[nums[i] - 1] != nums[i]Swap until current number is at correct index or invalid
nums[nums[i] - 1], nums[i] = nums[i], nums[nums[i] - 1]Place number at its correct index by swapping
public class Solution {
public static int firstMissingPositive(int[] nums) {
int n = nums.length;
for (int i = 0; i < n; i++) {
while (nums[i] >= 1 && nums[i] <= n && nums[nums[i] - 1] != nums[i]) {
int temp = nums[nums[i] - 1];
nums[nums[i] - 1] = nums[i];
nums[i] = temp;
}
}
for (int i = 0; i < n; i++) {
if (nums[i] != i + 1) return i + 1;
}
return n + 1;
}
public static void main(String[] args) {
System.out.println(firstMissingPositive(new int[]{3,4,-1,1})); // 2
System.out.println(firstMissingPositive(new int[]{1,2,0})); // 3
}
}
Line Notes
int n = nums.length;Store array length for bounds checking
for (int i = 0; i < n; i++)Iterate over array to place elements correctly
while (nums[i] >= 1 && nums[i] <= n && nums[nums[i] - 1] != nums[i])Swap until element is at correct position or invalid
int temp = nums[nums[i] - 1];Temporary variable for swapping elements
#include <iostream>
#include <vector>
using namespace std;
int firstMissingPositive(vector<int>& nums) {
int n = nums.size();
for (int i = 0; i < n; i++) {
while (nums[i] >= 1 && nums[i] <= n && nums[nums[i] - 1] != nums[i]) {
swap(nums[i], nums[nums[i] - 1]);
}
}
for (int i = 0; i < n; i++) {
if (nums[i] != i + 1) return i + 1;
}
return n + 1;
}
int main() {
vector<int> nums1 = {3,4,-1,1};
vector<int> nums2 = {1,2,0};
cout << firstMissingPositive(nums1) << endl; // 2
cout << firstMissingPositive(nums2) << endl; // 3
return 0;
}
Line Notes
int n = nums.size();Store array size for bounds checking
for (int i = 0; i < n; i++)Iterate over array to place elements correctly
while (nums[i] >= 1 && nums[i] <= n && nums[nums[i] - 1] != nums[i])Swap until element is at correct position or invalid
swap(nums[i], nums[nums[i] - 1]);Swap elements to place them correctly
function firstMissingPositive(nums) {
const n = nums.length;
for (let i = 0; i < n; i++) {
while (nums[i] >= 1 && nums[i] <= n && nums[nums[i] - 1] !== nums[i]) {
let temp = nums[nums[i] - 1];
nums[nums[i] - 1] = nums[i];
nums[i] = temp;
}
}
for (let i = 0; i < n; i++) {
if (nums[i] !== i + 1) return i + 1;
}
return n + 1;
}
// Test cases
console.log(firstMissingPositive([3,4,-1,1])); // 2
console.log(firstMissingPositive([1,2,0])); // 3
Line Notes
const n = nums.length;Store array length for bounds checking
for (let i = 0; i < n; i++)Iterate over array to place elements correctly
while (nums[i] >= 1 && nums[i] <= n && nums[nums[i] - 1] !== nums[i])Swap until element is at correct position or invalid
let temp = nums[nums[i] - 1];Temporary variable for swapping elements
Each number is swapped at most once to its correct position, so total operations are linear in the size of the array.
💡 For n=20, this means about 20 swaps and scans, making it very efficient compared to other approaches.
Interview Verdict: Accepted and optimal
This is the best approach to implement in interviews for this problem.