Given an unsorted integer array, find the smallest missing positive integer in it.

For example,

Input:  {1, 4, 2, -1, 6, 5}
Output: 3
 
Input:  {1, 2, 3, 4}
Output: 5

Practice this problem

A simple solution would be to search for all positive numbers in the given array, starting from 1. The time complexity of this solution is O(n2) since the first missing positive number must lie within the range [1, n+1] in an array of size n.

 
The time complexity can be improved using sorting. The idea is to sort the array and perform a linear scan of the sorted array to find the first missing positive number. The time complexity of this solution is O(n.log(n)). However, the solution is far from optimal. This post provides an overview of some available alternatives to solve this problem in O(n) time.

1. Using Hashing

The idea is to insert all elements (or only positive integers) in the array into a hash set. Like the brute-force approach, do a lookup for positive numbers in the hash set, starting from 1. The smallest positive number missing from the hash set is the result.

The time complexity of this solution is O(n), but requires O(n) extra space for the hash set. Following is the C, C++, Java, and Python program that demonstrates it:

C


Download  Run Code

Output:

The smallest missing positive number from the array is 3

C++


Download  Run Code

Output:

The smallest missing positive number from the array is 3

Java


Download  Run Code

Output:

The smallest missing positive number from the array is 3

Python


Download  Run Code

Output:

The smallest missing positive number from the array is 3

2. Using Partitioning logic of Quicksort

The idea is to segregate positive and negative numbers. We can easily do this in linear time and constant space using the Quicksort algorithm’s partitioning technique. The idea is to use 0 as a pivot element and make one pass of the partition process. After the partition step, all positive numbers are put together on one side of the array. The idea is to ignore all non-positive elements and process the subarray containing all positive elements, say nums[0, k-1] for pivot index k.

We initially check if the smallest missing number lies in range 1 to k. To check whether the smallest missing number lies in range 1 to k, use array elements as index and mark array elements as negative, i.e., for each subarray element x, we get the absolute value of the element abs(x) and make the element at index abs(x)-1 negative.

Finally, traverse the subarray once again and find the first index, which has a positive value. If a positive number is located at index i, then the smallest missing number is i+1. If no positive is found, then the smallest missing number must be k+1.

Note that this method modifies the original array. To keep the original array intact, run this approach on an auxiliary array. The algorithm can be implemented in C++, Java, and Python, as shown below:

C++


Download  Run Code

Output:

The smallest positive missing number is 3

Java


Download  Run Code

Output:

The smallest positive missing number is 3

Python


Download  Run Code

Output:

The smallest positive missing number is 3

The time complexity of the above solution is O(n) and doesn’t require any extra space, where n is the size of the input.