Skip to content
Related Articles
Find the smallest positive number missing from an unsorted array | Set 3
• Difficulty Level : Easy
• Last Updated : 07 Jun, 2021

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:

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.

## C++

 `// CPP program to find the smallest``// positive missing number``#include ``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);` `    ``// Function call``    ``cout << findMissingPositive(arr, n);` `    ``return` `0;``}`

## Java

 `// 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 x = ``new` `HashSet();` `    ``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`

## Python3

 `# 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`

## C#

 `// 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`

## Javascript

 ``
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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live

My Personal Notes arrow_drop_up