Solution Review: Smallest Positive Missing Number
This review provides a detailed analysis of finding the smallest positive missing number in an unsorted array.
First solution
Using a brute force method, we check if each number exists in the list. We compare each number starting from 1 to the array’s length and check if it’s present in the array. The first missing number is returned. If all of the numbers are present, then -1 is returned.
Let’s check the illustration below to better understand the solution.
Solution code
Time complexity
In this case, the time complexity is .
Explanation
-
Line 5: We initialize the
foundflag with afalsevalue. -
Lines 6–17: The outer for loop generates the number sequence from
1 to nand passes it to the innerforloop.- Line 7: At the start of each iteration, we reset this
foundflag. - Line 14–16: At the
- Line 7: At the start of each iteration, we reset this