Find The Smallest Positive Number Missing From An Unsorted Array
Coding Interview Problem

There are multiple algorithms in programming to solve a given problem. However, we always need to choose the best optimal solution out of the lot!
In data structures and algorithms, arrays, unsorted arrays, intersection of array form an essential part. As an array is a data structure that holds multiple instances of a particular data type, you can access it with the help of index.
Though, in certain cases, you may also encounter an unsorted array where the values may be placed before you in some sort of arbitrary order.
Here, sometimes, you have to find smallest positive numbers or first missing positive in your unsorted array. For this, you have to follow certain approaches.
Hence, for help we will be discussing this problem in detail. So without any further ado, let’s get started!
Problem Statement
Let’s first start with a problem statement. The task given to you is to find the smallest first missing positive integers of an unsorted array ARR with the length N which contains all positive integers and negative integers.
Note: You can modify the array
ARR [5]
4 -1 0 3 4
0 1 2 3 4
In this case, smallest missing integer would be 1
Approaches to find the missing smallest positive number
To find the smallest positive numbers in an unsorted array, you can apply various approaches. With the help of the above-mentioned problem statement, we will go into the detailing of finding the missing integers.
Brute Force Approach
This is one of the best approaches to find smallest positive integers. Since, the total number of elements in a given array N. Thus, the smallest positive integers missing from your array, lie in 1, N+1 integers.
In brute force approach, the total number of elements present in an array are N and the smallest positive integer will also lie in the range of [ 1, n+1 ] integers
Let us consider with the help of an example;
an unsorted array named ARR of length .7 is given to you as it contains both positive and negative integers.
Since N = 7 and we are going to start from the i which is 1. Then we will move on till the time we reach the i = 7. This would be the nth number.
If we have to find all numbers which are less than or equal to the 7, the answer will turn out to be 8.
Therefore, for every i you traverse through your array of two loops, find if it is present in a given array or not. In this case, we have to find the missing number i.e 4.
Input
Enter number of present elements
7
Enter all elements
-1
5
0
1
3
7
2
Output
The smallest first missing positive integer in this case is 4
Time Complexity
As we traverse entire array for every i range 1+ n + 1, the time complexity of this case would be O [ n*n]
Space Complexity
As no extra space will be used here, space complexity here would be O [1]
Sorting Approach
Another approach you can use to solve problems in a better time complexity would be the sorting approach.
First we will have to sort an array. The best way to do this is by either using the inbuilt function or using the merge sorting sorting algorithm. The reason why we use merge sort is because it gives ideal time complexity even if the elements are in their worst cases.
You can also use other algorithms like insertion sort or quick sort. The forward step here would be to find the positive integers first. After this, you can differentiate two consecutive numbers. If the difference will come out to be greater than 1, it will be clear that one number is missing in these elements.
Therefore, the answer 1 will then be added to this small number.
For this let consider the unsorted array of length 7
ARR [7]
-`1, 5, 0, 1, 7, 3, 2
0 1 2 3 4 5 6
First sort the array
-`1 0 1 2 3 4 5 7
0 1 2 3 4 5 6
The positive integer will lie at index 2
Linearly traverse array from this index 2 and calculate difference between these positive integers.
When difference is greater than 1 and is missing, the number is given by 3 +1 = 4
Time Complexity
To calculate time complexity you have to first sort an array with O [N * {log N}]
After that you need to traverse the entire array by using the first index of your positive integers. You can also use it to find the missing positive integer at the end.
Space Complexity
Since no additional space will be used, you can use the space complexity; O [N]
Hashing or Indexing Approach
In order to solve your array’s time complexity, you can use hashing or indexing approaches.
Iterate the array linearly to mark all positive numbers in your hashmap. Traverse it from 1 to n+1 to check if value on the keys turns out to be true or not. If the value turns out to be false, numbers are not present in an array. Whenever you see a false, you can break the loop to find missing positive integers.
Time complexity
In this case, we will first iterate an array to store all numbers in a hashmap or to insert all elements into an hashmap. In such cases, insert N elements into hashmap. Then the operation would come out to be of O[N] times.
Space complexity
So there will be no additional space used to create a hashmap, space complexity would also be O[N] where n denotes a set of positive integers.
Wrapping Up
The concept of array, unsorted array, intersection of arrays holds great importance for all programmers! In this blog we covered different approaches to find the smallest positive integer in an unsorted array.
Finding first missing positive integers would be easy if you apply the above-mentioned approaches.
About the Creator
Ishita Juneja
A professionally trained Tech Expert, with great experience in Data Science, SQL, Machine Learning, Python, Coding, Programming, and Deep Learning.



Comments
There are no comments for this story
Be the first to respond and start the conversation.