22. Smallest Positive Missing
β GFG solution to the Smallest Positive Missing Number problem: find the smallest missing positive integer using index marking technique with O(n) time and O(1) space. π
The problem can be found at the following link: π Question Link
π§© Problem Description
You are given an unsorted array arr[] of size N that can contain negative numbers and zeros. Your task is to find the smallest positive number missing from the array.
π Important Notes:
Positive integers start from
1.Ignore all values β€ 0 and values greater than the array size while computing the result.
The missing number must be positive and strictly greater than 0.
π Examples
Example 1
Input: arr[] = [2, -3, 4, 1, 1, 7]
Output: 3
Explanation: Smallest positive missing number is 3.
The array contains 1, 2, 4, 7 but missing 3.Example 2
Example 3
π Constraints
$1 \le \text{arr.size()} \le 10^5$
$-10^6 \le \text{arr}[i] \le 10^6$
β
My Approach
The optimal approach uses Index Marking technique to achieve O(n) time complexity with O(1) space complexity:
Index Marking Technique
Clean Invalid Numbers:
Replace all non-positive numbers (β€ 0) with a value greater than n (array size).
This ensures all numbers are positive and within a manageable range.
Mark Presence Using Signs:
For each number
xin the array, mark its presence by makingarr[x-1]negative.Use
abs(arr[i])to get the actual value since we're modifying signs.Only mark if
xis within valid range [1, n].
Find First Unmarked Position:
Traverse the array to find the first positive number.
The index of first positive number + 1 gives the smallest missing positive.
If all numbers are negative (marked), return n+1.
Key Insight:
We use the array itself as a hash map where index represents the number.
Negative sign indicates presence, positive indicates absence.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n), where n is the size of the array. We make three separate passes through the array: first to clean invalid numbers, second to mark presence, and third to find the first unmarked position.
Expected Auxiliary Space Complexity: O(1), as we only use the input array for marking and a few constant variables, requiring no additional space proportional to input size.
π§ Code (C)
π§βπ» Code (C++)
β‘ View Alternative Approaches with Code and Analysis
π 2οΈβ£ Set-Based Approach
π‘ Algorithm Steps:
Insert all positive numbers into an unordered set for O(1) lookup.
Starting from 1, check each positive integer sequentially.
Return the first integer not found in the set.
Continue until we find the missing positive number.
π Complexity Analysis:
Time: β±οΈ O(n) - Linear time for set creation and lookup
Auxiliary Space: πΎ O(n) - Additional space for the set
β
Why This Approach?
Simple and intuitive logic flow
Easy to understand and implement
Good for interview discussions and debugging
π 3οΈβ£ Sorting Approach
π‘ Algorithm Steps:
Sort the entire array to arrange elements in ascending order.
Skip all negative numbers and zeros at the beginning.
Track the expected positive number starting from 1.
Find the first gap in the positive sequence where expected number is missing.
π Complexity Analysis:
Time: β±οΈ O(n log n) - Due to sorting operation
Auxiliary Space: πΎ O(1) - Only constant extra space used
β
Why This Approach?
Space efficient with constant space usage
Handles duplicates naturally without extra logic
Clear sequential processing after sorting
π 4οΈβ£ Swap-Based Cyclic Sort
π‘ Algorithm Steps:
Use cyclic sort to place each positive number at its correct index position.
For each element, if it's positive and within range [1,n], swap it to position
arr[i]-1.Continue swapping until each element is in its correct position or out of range.
After sorting, find the first position where
arr[i] != i+1.
π Complexity Analysis:
Time: β±οΈ O(n) - Each element is moved at most once to its correct position
Auxiliary Space: πΎ O(1) - In-place sorting with constant space
β
Why This Approach?
Optimal time and space complexity combination
In-place arrangement without extra data structures
Classic cyclic sort pattern for missing number problems
π π Comparison of Approaches
π Approach
β±οΈ Time Complexity
πΎ Space Complexity
β Pros
β οΈ Cons
π·οΈ Index Marking
π’ O(n)
π’ O(1)
π Optimal time & space
π§ Modifies input array
π Set-Based
π’ O(n)
π‘ O(n)
π Easy to understand
πΎ Extra space required
π Sorting
π‘ O(n log n)
π’ O(1)
π― Handles duplicates well
π Slower time complexity
π Cyclic Sort
π’ O(n)
π’ O(1)
β Classic approach
π§ Complex swapping logic
π Best Choice Recommendation
π― Scenario
ποΈ Recommended Approach
π₯ Performance Rating
π Optimal performance needed
π₯ Index Marking
β β β β β
π Readability priority
π₯ Set-Based
β β β β β
π§ Cannot modify input
π₯ Sorting
β β β β β
π― Interview/Competitive
π Cyclic Sort
β β β β β
β Code (Java)
π Code (Python)
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated