Given an unsorted array with both positive and negative elements including 0. The task is to find the smallest positive number missing from the array in O(N) time.
Examples:
Input: arr[] = {-5, 2, 0, -1, -10, 15}
Output: 1
Input: arr[] = {0, 1, 2, 3, 4, 5}
Output: 6
Input: arr[] = {1, 1, 1, 0, -1, -2}
Output: 2
We can use hashing to solve this problem. The idea is to build a hash table of all positive elements in the given array. Once the hash table is built. We can look in the hash table for all positive integers, starting from 1. As soon as we find a number which is not there in the hash table, we return it.
We can use the unordered_map in C++ to implement the hash which allows performing look-up operation in almost O(1) time complexity.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int missingNumber( int a[], int n)
{
unordered_map< int , int > mp;
for ( int i = 0; i < n; i++) {
if (a[i] > 0)
mp[a[i]]++;
}
int index = 1;
while (1) {
if (mp.find(index) == mp.end()) {
return index;
}
index++;
}
}
int main()
{
int a[] = { 1, 1, 1, 0, -1, -2 };
int size = sizeof (a) / sizeof (a[0]);
cout << "Smallest positive missing number is : "
<< missingNumber(a, size) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int missingNumber( int a[], int n)
{
Map<Integer, Integer> mp = new LinkedHashMap<>();
for ( int i = 0 ; i < n; i++)
{
if (a[i] > 0 )
{
mp.put(a[i], mp.get(a[i]) == null ? 1 : mp.get(a[i]) + 1 );
}
}
int index = 1 ;
while ( true )
{
if (!mp.containsKey(index))
{
return index;
}
index++;
}
}
public static void main(String[] args)
{
int a[] = { 1 , 1 , 1 , 0 , - 1 , - 2 };
int size = a.length;
System.out.println( "Smallest positive missing number is : "
+ missingNumber(a, size));
}
}
|
Python3
def missingNumber(a, n) :
mp = dict ();
for i in range (n) :
if (a[i] > 0 ) :
if a[i] not in mp.keys() :
mp[a[i]] = 0
mp[a[i]] + = 1
index = 1 ;
while ( 1 ) :
if (index not in mp.keys()) :
return index;
index + = 1 ;
if __name__ = = "__main__" :
a = [ 1 , 1 , 1 , 0 , - 1 , - 2 ];
size = len (a);
print ( "Smallest positive missing number is : " ,missingNumber(a, size));
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int missingNumber( int []a, int n)
{
Dictionary< int , int > m = new Dictionary< int , int >();
for ( int i = 0; i < n; i++)
{
if (a[i] > 0)
{
if (m.ContainsKey(a[i]))
{
var val = m[a[i]];
m.Remove(a[i]);
m.Add(a[i], val + 1);
}
else
{
m.Add(a[i], 1);
}
}
}
int index = 1;
while ( true )
{
if (!m.ContainsKey(index))
{
return index;
}
index++;
}
}
public static void Main(String[] args)
{
int []a = {1, 1, 1, 0, -1, -2};
int size = a.Length;
Console.WriteLine( "Smallest positive missing number is : "
+ missingNumber(a, size));
}
}
|
Javascript
<script>
function missingNumber(a, n) {
let mp = new Map();
for (let i = 0; i < n; i++) {
if (a[i] > 0) {
mp[a[i]]++;
if (mp.has(a[i])) {
mp.set(a[i], mp.get(a[i]) + 1)
} else {
mp.set(a[i], 1)
}
}
}
let index = 1;
while (1) {
if (!mp.has(index)) {
return index;
}
index++;
}
}
let a = [1, 1, 1, 0, -1, -2];
let size = a.length;
document.write( "Smallest positive missing number is : " +
missingNumber(a, size) + "<br>" );
</script>
|
Output:
Smallest positive missing number is : 2
Time Complexity: O(N)
Auxiliary Space: O(N)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
08 Jun, 2021
Like Article
Save Article