Smallest Positive Number Missing in an Unsorted Array

Difficulty Level Hard
Frequently asked in Accolite Adobe Amazon Apple Bloomberg ByteDance Databricks eBay Facebook Factset Goldman Sachs Google JP Morgan Microsoft Morgan Stanley Oracle Salesforce Samsung Snapdeal Tencent Tesla Twitch Uber Walmart Labs Wish
Array Booking.comViews 4317

Problem Statement

In the given unsorted array find the smallest positive number missing in an unsorted array. A positive integer doesn’t include 0. We can modify the original array if needed. The array may contain positive and negative numbers.

Example

a. Input array : [3, 4, -1, 0, -2, 2, 1, 7, -8]

Output: 5.
In the given array 1,2,3,4 are present but 5 is absent, which is the smallest positive number missing in the given array.

b. Input array : [2, 3, 7, 8, -1, -2 ,0]

Output: 1.
In the given array 1 is absent, which is the smallest positive number missing in the given array.

c. Input array : [2, 3, 7, 8, -1, -2 ,0]

Output: 1.
In the given array 1 is absent, which is the smallest positive number missing in the given array.

Algorithm for Smallest Positive Number Missing in an Unsorted Array

1. We need to find the smallest missing positive number, so we don’t need negative numbers. So, we create a function to move all the positive numbers to the end of the array and we find the missing numbers in the positive array.

2. In positive_array function,

  • We traverse the array with two pointers, if we find a negative number we swap it with the start pointer and move it forward.
  • We continue this until it reaches the end of the array and we return the start index of the positive array. Which is the start pointer position, when we reach the end of the array.

3. We create a FindMissingPostive function that takes the array and size of the array and gives the smallest missing number.

4. In this function,

  • a. In the input array, if we find a positive integer, we mark it visited my making the value at its index position to negative. Example: If the given array is array[]. In this array, if we find 2 we make array[1]  = -2 and so on till the end of array only if array[i] – 1< size and array[array[i] – 1]> 0.
  • After modifying the array. If the index at which we find the first positive number is k. Then k+1 is the smallest missing number. If we didn’t find a positive number then, the size of the array + 1 is the smallest missing number.

5. For the given input array, we first apply positive_arrayfunction(let it return j)and we apply the  FindMissingPostive on (array+j).

Implementation

C++ Program for Smallest Positive Number Missing in an Unsorted Array

#include <stdio.h>
#include <stdlib.h>
#include <bits/stdc++.h>
using namespace std;
 
//function to swap addresses
void swap(int *a, int *b)
{
    int temp = *a;
    *a   = *b;
    *b   = temp;
}
//In this function we move non-postive elements to the start of the array
//return the start index of start index of postive array
int positive_array(int array[], int N)
{
    int start_index = 0, i;
    for(i = 0; i < N; i++)
    {
       if (array[i] <= 0)  
       {
           swap(&array[i], &array[start_index]);
           start_index++;  // increment count of non-positive integers
       }
    }
    return start_index;
}
//In the given array this function finds the smallest missing number
//N is size of the array
//we mark the elements by making the elements at the postion(i-1)to negitive
//after modifying the array we find index at which we find first postive(j) and j+1 is the missing number.
int FindMissingPostive(int array[], int N)
{
  for(int i = 0; i < N; i++)
  {
    if(abs(array[i]) < N+1 && array[abs(array[i]) - 1] > 0)
    {
      array[abs(array[i]) - 1] = -array[abs(array[i]) - 1];
    }
  }
  for(int k = 0; k < N; k++){
    if (array[k] > 0)
    {
      return k+1;
    }
  }
  return N+1;
}
//insted of finding the missing postive integer in whole array
//we can find in postive part of the array fast.
int FindMissing(int array[], int N)
{
   int start = positive_array(array,N);
 
   return FindMissingPostive(array+start,N-start);
}
//main function to test above functions 
int main()
{
  int N;//size of the array
  cin>>N;
  int a[N];
  for(int i=0;i<N;i++)
  {
      cin>>a[i];
  }
  cout<<"smallest positive number missing: = "<<FindMissing(a,N);
  return 0;
}

Java Program for Smallest Positive Number Missing in an Unsorted Array

import java.util.Scanner;

class sum
{
    public static int abs(int x)
    {
        if(x<0)
        {
            return -1*x;
        }
        else
        {
            return x;
        }
    }
    //In this function we move non-postive elements to the start of the array
    //return the start index of start index of postive array
    public static int positive_array(int array[], int N)
    {
        int start_index = 0, i;
        for(i = 0; i < N; i++)
        {
           if (array[i] <= 0)  
           {
               swap(array[i], array[start_index]);
               start_index++;  // increment count of non-positive integers
           }
        }
        return start_index;
    }
    //In the given array this function finds the smallest missing number
    //N is size of the array
    //we mark the elements by making the elements at the postion(i-1)to negitive
    //after modifying the array we find index at which we find first postive(j) and j+1 is the missing number.
    public static int FindMissingPostive(int array[], int N)
    {
      for(int i = 0; i < N; i++)
      {
        if(abs(array[i]) < N+1 && array[abs(array[i]) - 1] > 0)
        {
          array[abs(array[i]) - 1] = -array[abs(array[i]) - 1];
        }
      }
      for(int k = 0; k < N; k++){
        if (array[k] > 0)
        {
          return k+1;
        }
      }
      return N+1;
    }
    //insted of finding the missing postive integer in whole array
    //we can find in postive part of the array fast.
    public static int FindMissing(int array[], int N)
    {
       int start = 0;
        for(int i = 0; i < N; i++)
        {
           if (array[i] <= 0)  
           {
               array[i]=array[i]+array[start]-(array[start]=array[i]);
               start++;  // increment count of non-positive integers
           }
        }
       int temp[] = new int[N-start];
       for(int i=0;i<N-start;i++)
       {
           temp[i]=array[i+start];
       }
       return FindMissingPostive(temp,N-start);
    }
    public static void main(String[] args)  
    { 
        Scanner sr = new Scanner(System.in);
        int n = sr.nextInt();
        int a[] = new int[n];
        for(int i=0;i<n;i++)
        {
            a[i] = sr.nextInt();
        }
        System.out.println("smallest positive number missing: = "+FindMissing(a,n));
    }
}
9
3 4 -1 0 -2 2 1 7 -8
smallest positive number missing: = 5

Complexity Analysis

Time Complexity

O(n) where n is the size of the given array. Here we traverse the array only one time and got the smallest positive missing number.

Space Complexity

O(1) because here we use some variables to calculate the answer.

References

Translate »