Find the Smallest Positive Number Missing From an Unsorted Array


Our objective is to find the smallest positive number that is missing from an unsorted array. We will be given an array a[] of both positive and negative numbers, we need to get the smallest positive number that is missing from an unsorted array in this problem. We can modify the array given in this problem to solve it.

For example,

INPUT : a[] = {5, 8, -13, 0, 18, 1, 3}

OUTPUT : 2

INPUT : a[] = {7, 10, -8, 1, 4}

OUTPUT : 2

In the above examples, we are given an unsorted array as an input. The smallest positive integer missing in the array is our output.

Naive Approach

The naive approach to this problem can be searching over the given array a[] for all positive numbers starting with 1. But the approach is not very efficient as in the worst case we may have to search for at most n+1 times which will require n number of iterations.

The time complexity of this approach can be O(N^2) in the worst case and space complexity will be O(1) as no extra space is required.

Approach

Approach-1 (Marking Elements)

In this approach, we will find the smallest positive number missing from an unsorted array by marking the elements present in the array.

The logic behind this approach is to mark the elements present in the given array in another array. Then traverse the array of marked elements and return the first element that isn’t marked. And if all the elements are marked return (size of array) + 1.

Below is the step-by-step illustration of the above approach −

  • We will initialize a function to get the smallest positive number missing from the array.

  • If the size of the given array is n, then initialize a new array of size n+1 to mark the presence of elements.

  • We will initialize the new array of boolean data type with false at every index.

  • Now iterating over the original given array we will check if the ith number is a positive number and less than or equal to n which is the size of the array. If it is then change the index value of the new array of ith number to true.

  • After this, just iterate over the new updated array of marked elements from 1 and return the (index value+1) at which we will first encounter false which will be the smallest positive number missing from the array.

  • If we don’t come across any false while iterating the array of marked elements just return n+1 where n is the size of the given unsorted array.

Example

Below is the implementation of the above approach in C++ −

#include <iostream>
#include <bits/stdc++.h>

using namespace std;

//function to find the smallest positive number from the unsorted array
int positiveMissingElement(int a[],int N){
   bool isPresent[N+1]={false}; //to mark the presence of elements which are in the given array
   
   for(int i=0;i<N;i++) //iterating over the given array to mark the presence of elements
   {
      if(a[i]>0 && a[i]<=N) //to check if number is a positive number and to check if it is less than or equal to n
      {
         isPresent[a[i]]=true;
      }
   }
   
   for(int i=1;i<=N;i++)  //to find the first index which is false as it will be the number which is missing in the original array
   {
      if(isPresent[i]==false){
         return i;
      }
   }
   
   return N+1;
}

//Driver Code
int main(){
   int a[]={ 0, 10, 4, -1, -18 ,1,2};
   int S = sizeof(a) / sizeof(a[0]);
   int ans=positiveMissingElement(a,S);
   cout<<"Missing Positive Element : "<<ans<<endl;
   return 0;
}

Output

Missing Positive Element : 3

Time Complexity : O(N), since we are only doing n traversal two times.

Space Complexity : O(N), since we created an array of size n+1.

Approach-2 (Changing the Input Array)

In this approach we will be changing the input array by performing certain conditional operations to get the required answer. Basically, we will first mark all the elements present in the given array which are greater than n(size of the given array) and less than 1 as 1. We will be doing this because the answer always lies between [1,n+1].

Below are the steps that we need to follow for this approach −

  • Since 1 is the smallest positive integer that could be missing from the given array. So we will first iterate through the given array to see if 1 is there. As it is the smallest positive number missing from the input array, we will return 1 if it is absent.

  • Repeat the input array's traversal if it's there. Make every integer less than 1 or more than n to 1 while traversing the array again since the largest possible answer can be n+1, where n is the size of the input array.

  • After this again traverse the array. This time for every ith number we will update the value of a[(a[i]-1)%n] by adding n to it which is the size of the array. Here we take modulo n because in some cases the value of index can go greater than the size of the array.

  • The first index where value will be less than N, we will add 1 in that index value and return it as that would be our required result.

Example

Below is the implementation of the above approach in C++ −

#include <iostream>
#include <bits/stdc++.h>

using namespace std;

//function to find the minimum positive number missing from an array
int positiveMissingElement(int a[],int N){
   bool onePresent=false;
   
   for(int i=0;i<N;i++) //to check if 1 is there in the array
   {
      if(a[i]==1){
         onePresent=true;
         break;
      }
   }
   
   if(onePresent==false) //if 1 is missing then return 1
   {
      return 1;   
   }
   for(int i=0;i<N;i++) //to mark all the elements greater than N and less than 1 as 1
   {
      if(a[i]<=0||a[i]>=N){
         a[i]=1;
      }
   }
   
   for(int i=0;i<N;i++) //to update the values of indices according to the number present in the array
   {
      int temp=(a[i]-1)%N;
      a[temp]=a[temp]+N;
   }
   
   for(int i=0;i<N;i++) //to find the index with value less than or equal to N
   {
      if(a[i]<=N){
         return i+1;
      }
   }
   return N+1; 
}
int main(){
   int a[]={ 0, 10,3, -12, -2 ,1,2,5,6,4,8};
   int S = sizeof(a) / sizeof(a[0]);
   int ans=positiveMissingElement(a,S);
   cout<<"Missing Positive Element : "<<ans<<endl;
   return 0;
}

Output

Missing Positive Element : 7

Time Complexity: O(N), since we are just traversing over the input array.

Space Complexity: O(1), since we use no extra space.

Approach-3 (Using Swapping)

In this approach, we will be using swapping to get our output. Swap is a standard library in c++ used to swap two elements in an array with respect to their positions in the array. We are using a bit of the concept of the above approach in this too.

  • Since the answer lies between 1 and n+1 so for every element greater than or equal to 1 and less than or equal to n, we will check if it is on the appropriate index i.e. a[i] is equal to a[a[i]-1]. If not then we will swap both the elements.

  • Now we will iterate in the input array and check if the element present at every index is equal to 1 + index value. If not, return i+1.

  • If all the elements present in the array are equal to (index value+1), return n+1.

Example

Below is the implementation of the above approach in C++ −

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

//function to find minimum positive number missing from the array
int positiveMissingElement(int a[],int N){
   for(int i=0;i<N;i++){
      while(a[i]>0 && a[i]<=N && a[i]!=a[a[i]-1]){ //for every element to be at right index
         swap(a[i],a[a[i]-1]);
      }
   }
   
   for(int i=0;i<N;i++) //for checking every element and their indices
   {
      if(a[i]!=i+1) //if not equal then return 1+index value
      {
         return i+1;
      }
   }
   
   return N+1; //return n+1 if every element is at right index
}
int main(){
   int a[]={10,3,-5,1,5,-20,2,7,4};
   int S = sizeof(a) / sizeof(a[0]);
   int ans=positiveMissingElement(a,S);
   cout<<"Missing Positive Element : "<<ans<<endl;
   return 0;
}

Output

Missing Positive Element : 6

Time Complexity : O(N) , since we are only traversing the input array two times.

Space Complexity : O(1) , since we don’t use any extra space.

Approach-4 (Using Sorting)

In this approach, we are going to use a STL library present in C++ which is sort used to sort the array in ascending order. The basic idea of this approach is that we will sort the array and then check if the smallest positive number is present i.e 1. If it is present then increment it by 1 and then again check until we traverse the whole array. Below are the steps that we are going to follow in this approach −

  • First we will sort the array.

  • Now declare a variable named temp and store 1 in it which is the smallest positive number which could be missing from the array.

  • Now we will traverse the array from 0 and check at every index if value equals 1.

  • If the value equals to temp, then we will increase temp by 1 and again check if at any index the value equals to temp until the size of the array i.e. n.

  • After iterating once the given array, the smallest missing number will be the number stored in variable temp.

  • Return temp which will be our required output.

Example

Below is the implementation of the above approach in C++ −

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

//function to find the smallest positive number missing from an array
int positiveMissingElement(int a[],int N){
   sort(a,a+N); //using sort STL to sort the array
   
   int temp=1; //to check if the number is present in the array
   
   for(int i=0;i<N;i++){
      if(a[i]==temp) { //if element is present in the array increase temp by 1
         temp++;
      }
   }
   
   return temp;
}

//Driver Code
int main(){
   int a[]={ 0,2,10,3,-10,-20,1,6,4};
   int S = sizeof(a) / sizeof(a[0]);
   int ans=positiveMissingElement(a,S);
   cout<<"Missing Positive Element : "<<ans<<endl;
   return 0;
}

Output

Missing Positive Element : 5

Time Complexity: O(N*logN), since it is the time complexity of the sorting function.

Space Complexity: O(1), since we don’t use any extra space.

Conclusion

In this article, we have learned to solve the problem of finding the smallest positive number missing from an unsorted array with different approaches. We used four different approaches to solve the above problem.

I hope you find this article helpful in solving all your doubts regarding the problem.

Updated on: 14-Mar-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements