Solution: First Missing Positive
Let's solve the First Missing Positive problem using the Cyclic sort pattern.
Statement#
Given an unsorted integer array, nums, return the smallest missing positive integer.
Create an algorithm that runs with an time complexity and utilizes a constant amount of space.
Note: The smallest missing positive isn’t the first positive number that’s missing in the range of elements in the input, but the first positive number that’s missing if we start from .
Constraints:
-
nums.length -
nums[i]
Solution#
So far, you have probably brainstormed some approaches and have an idea of how to solve this problem. Let’s explore some of these approaches and figure out which one to follow based on considerations such as time complexity and any implementation constraints.
Naive approach#
The brute force approach would be to search for every positive integer in the array, starting from
and incrementing the number to be searched until we find the first missing positive number. It will result in an algorithm with the time complexity of . A solution that everyone thinks about is sorting the array and then searching for the smallest positive number. However, this also gives us a time complexity of
where n is the number of elements present in our array. The final approach we would discuss is storing all the numbers in a hash table and returning the first positive value we don't encounter. This solution would fulfill the time complexity condition of
but still requires extra space. That's where cyclic sort comes in.
Optimized approach using cyclic sort#
An array of size containing numbers ranging from to can be sorted using the cyclic sort approach in . Here, we have two problems. First, the array may have negative numbers as well. Second, the array does not have all consecutive numbers. The first problem can be solved by simply ignoring the negative values. The second problem can be solved by linearly scanning the sorted array in . So, overall, our problem is solved in time and space, because cyclic sort sorts the elements in-place with extra space. Now, we can proceed further and discuss our algorithm.
The algorithm iterates over each element of the array, and for each element, it determines the correct position where the element should be placed. It does this by subtracting from the element value because of 0-based indexing. It checks the following conditions for each element after determining its correct position:
-
If the current element is in the range, and if it is not already at its correct position, swap the current element with the element at its correct position.
-
Otherwise, move on to the next element.
After placing all the elements at the correct positions, traverse the array again and check if the value at each index is equal to its index plus . If it is not, return the index plus as the smallest missing positive integer.
If all the values are in order, return the length of the array plus 1 as the smallest missing positive integer.
Let’s look at the following example to understand this in a better way:
1 of 10
2 of 10
3 of 10
4 of 10
5 of 10
6 of 10
7 of 10
8 of 10
9 of 10
10 of 10
Let’s look at the implementation of the solution discussed above:
Solution summary#
-
Iterate over all the elements in
numsand swap them with their correct position until all elements are in their correct positions. -
Iterate over
numsagain and check if each element is equal to its index plus 1. -
If an element is not in the correct position, return the index plus 1, of the element that is out of order, as the smallest missing positive number.
-
If all elements are in order, return the length of the array plus 1 as the smallest missing positive integer.
Time complexity#
The algorithm traverses over the array two times, and in both traversals, it iterates over each element exactly once. Therefore, the time complexity of this algorithm is , where is the length of the nums.
Space complexity#
The space complexity of the algorithm is as it uses only a constant amount of space.
First Missing Positive
Topological Sort: Introduction