Introduction
The First Missing Positive problem involves finding the smallest positive integer that is missing from the array.
Given an unsorted integer array arr[], the task is to find the first missing positive number.
The solution should work efficiently in:
- linear time
- constant extra space
This is one of the most important advanced Hashing and Array Manipulation problems and helps in understanding index mapping, cyclic placement, and in-place hashing techniques.
Example
Input: arr[] = [1, 2, 0]Output: 3
Explanation: Positive numbers present are:
1, 2
The smallest missing positive number is 3.
Input: arr[] = [3, 4, -1, 1]
Output: 2
Explanation:
Positive numbers present are:
1, 3, 4
The smallest missing positive number is 2.
Constraints
1 <= n <= 10^5-10^9 <= arr[i] <= 10^9
Approach 1 : Brute Force (HashSet)
Explanation
The simplest way to solve this problem is:
- Store all positive elements in a HashSet
- Start checking numbers from 1.
- The first missing number is the answer
This approach is easy to understand but uses extra space.
Steps
- Create a HashSet.
- Store all positive numbers.
- Start checking integers from 1.
- If a number is missing:
- return it
- Continue until missing number is found.
Dry Run:
Input Array: [3, 4, -1, 1]Store positive numbers:
{1, 3, 4}
Check 1:
Present
Check 2:
Missing
Return 2
Brute Force Code
Complexity Analysis
Time Complexity: O(n)
Explanation:
Each element is inserted and checked once.
Space Complexity: O(n)
Explanation:HashSet stores positive numbers.
Approach 2 : Optimized Solution (Cyclic Placement)
Explanation
Instead of using extra space, we can place every positive number at its correct index.
The idea is:
Value x should be placed at index (x - 1)For example:1 should be at index 02 should be at index 1
3 should be at index 2
After placing elements correctly:
- the first index where:
- arr[i] != i + 1
- gives the missing positive number
This technique is called:
- cyclic sort
- index mapping
- in-place hashing
Steps
- Traverse the array.
- While current element is:
- positive
- within range
- not already at correct position
- swap it to correct index
- Traverse again.
- First incorrect index gives answer.
- If all positions are correct:
- return n+1
Dry Run
Input Array: [3, 4, -1, 1]Traverse 3:
Correct index for 3 = 2
Swap 3 and -1
Array:
[-1, 4, 3, 1]
Traverse 4:
Correct index for 4 = 3
Swap 4 and 1
Array:
[-1, 1, 3, 4]
Traverse 1:
Correct index for 1 = 0
Swap 1 and -1
Array:
[1, -1, 3, 4]
Traverse array again:
Index 0 → correct
Index 1 → arr[1] != 2
First Missing Positive = 2
Optimized Code
Complexity Analysis
Time Complexity: O(n)
Explanation:
Each element is placed at correct position at most once checked once.
Space Complexity: O(1)
Explanation:No extra data structures are used.
Array is modified in-place.
Time Complexity: O(n)
Explanation:
Each element is placed at correct position at most once checked once.
Space Complexity: O(1)
Explanation:No extra data structures are used.
Array is modified in-place.