Find the smallest positive number missing from an unsorted array | Set 3

Given an unsorted array with both positive and negative elements. The task is to find the smallest positive number missing from the array.

Examples:

Input: arr[] = {2, 3, 7, 6, 8, -1, -10, 15}
Output: 1

Input: arr[] = { 2, 3, -7, 6, 8, 1, -10, 15 }
Output: 4

Input: arr[] = {1, 1, 0, -1, -2}
Output: 2



Approach :
We have already discussed some of the techniques to find the smallest positive number missing from an unsorted array.

Find the smallest positive number missing from an unsorted array | Set 1
Find the smallest positive number missing from an unsorted array | Set 2

Here, we use set to store all positive integers and find the first missing positive integer.

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find the smallest
// positive missing number
#include <bits/stdc++.h>
using namespace std;
  
// Function to find the smallest
// positive missing number
int findMissingPositive(int arr[], int n)
{
    // Default smallest Positive Integer
    int m = 1;
  
    // Store values in set which are
    // greater than variable m
    set<int> x;
  
    for (int i = 0; i < n; i++) {
        // Store value when m is less than
        // current index of given array
        if (m < arr[i]) {
            x.insert(arr[i]);
        }
        else if (m == arr[i]) {
            // Increment m when it is equal
            // to current element
            m = m + 1;
  
            while (x.count(m)) {
                x.erase(m);
  
                // Increment m when it is one of the
                // element of the set
                m = m + 1;
            }
        }
    }
  
    // Return the required answer
    return m;
}
  
// Driver code
int main()
{
    int arr[] = { 2, 3, -7, 6, 8, 1, -10, 15 };
  
    int n = sizeof(arr) / sizeof(arr[0]);
  
    // Function call
    cout << findMissingPositive(arr, n);
  
    return 0;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the smallest
// positive missing number
import java.util.*;
  
class GFG
{
  
// Function to find the smallest
// positive missing number
static int findMissingPositive(int arr[], int n)
{
    // Default smallest Positive Integer
    int m = 1;
  
    // Store values in set which are
    // greater than variable m
    HashSet<Integer> x = new HashSet<Integer>();
  
    for (int i = 0; i < n; i++)
    {
        // Store value when m is less than
        // current index of given array
        if (m < arr[i])
        {
            x.add(arr[i]);
        }
        else if (m == arr[i])
        {
            // Increment m when it is equal
            // to current element
            m = m + 1;
  
            while (x.contains(m)) 
            {
                x.remove(m);
  
                // Increment m when it is one of the
                // element of the set
                m = m + 1;
            }
        }
    }
  
    // Return the required answer
    return m;
}
  
// Driver code
public static void main(String[] args) 
{
    int arr[] = { 2, 3, -7, 6, 8, 1, -10, 15 };
  
    int n = arr.length;
  
    // Function call
    System.out.println(findMissingPositive(arr, n));
}
}
  
// This code is contributed by 29AjayKumar
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 program to find the smallest
# positive missing number
  
# Function to find the smallest
# positive missing number
def findMissingPositive(arr, n):
      
    # Default smallest Positive Integer
    m = 1
  
    # Store values in set which are
    # greater than variable m
    x = []
    for i in range(n):
          
        # Store value when m is less than
        # current index of given array
        if (m < arr[i]):
            x.append(arr[i])
      
        elif (m == arr[i]):
              
            # Increment m when it is equal
            # to current element
            m = m + 1
  
            while (x.count(m)):
                x.remove(m)
  
                # Increment m when it is one of the
                # element of the set
                m = m + 1
  
    # Return the required answer
    return m
  
# Driver code
if __name__ == '__main__':
    arr = [2, 3, -7, 6, 8, 1, -10, 15]
  
    n = len(arr)
  
    # Function call
    print(findMissingPositive(arr, n))
  
# This code is contributed by
# Surendra_Gangwar
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the smallest
// positive missing number
using System;
using System.Collections.Generic;
      
class GFG
{
  
// Function to find the smallest
// positive missing number
static int findMissingPositive(int []arr, int n)
{
    // Default smallest Positive Integer
    int m = 1;
  
    // Store values in set which are
    // greater than variable m
    HashSet<int> x = new HashSet<int>();
  
    for (int i = 0; i < n; i++)
    {
        // Store value when m is less than
        // current index of given array
        if (m < arr[i])
        {
            x.Add(arr[i]);
        }
          
        else if (m == arr[i])
        {
            // Increment m when it is equal
            // to current element
            m = m + 1;
  
            while (x.Contains(m)) 
            {
                x.Remove(m);
  
                // Increment m when it is one of the
                // element of the set
                m = m + 1;
            }
        }
    }
  
    // Return the required answer
    return m;
}
  
// Driver code
public static void Main(String[] args) 
{
    int []arr = { 2, 3, -7, 6, 8, 1, -10, 15 };
  
    int n = arr.Length;
  
    // Function call
    Console.WriteLine(findMissingPositive(arr, n));
}
}
  
// This code is contributed by Rajput-Ji
chevron_right

Output:
4

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.





I have 7 years of experience as an EDI Developer

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :
Practice Tags :