Find the smallest positive number missing from an unsorted array : Hashing Implementation
Last Updated :
08 Jun, 2021
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++
// C++ program to find the smallest
// positive missing number
#include <bits/stdc++.h>
using namespace std;
// Function to find the smallest positive
// missing number
int missingNumber(int a[], int n)
{
// Declaring an unordered_map
unordered_map<int, int> mp;
// if array value is positive
// store it in map
for (int i = 0; i < n; i++) {
if (a[i] > 0)
mp[a[i]]++;
}
// index value set to 1
int index = 1;
// Return the first value starting
// from 1 which does not exists in map
while (1) {
if (mp.find(index) == mp.end()) {
return index;
}
index++;
}
}
// Driver code
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
// Java program to find the smallest
// positive missing number
import java.util.*;
class GFG
{
// Function to find the smallest positive
// missing number
static int missingNumber(int a[], int n)
{
// Declaring an unordered_map
Map<Integer, Integer> mp = new LinkedHashMap<>();
// if array value is positive
// store it in map
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);
}
}
// index value set to 1
int index = 1;
// Return the first value starting
// from 1 which does not exists in map
while (true)
{
if (!mp.containsKey(index))
{
return index;
}
index++;
}
}
// Driver code
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));
}
}
// This code has been contributed by 29AjayKumar
Python3
# Python3 program to find the smallest
# positive missing number
# Function to find the smallest positive
# missing number
def missingNumber(a, n) :
# Declaring an unordered_map
mp = dict();
# if array value is positive
# store it in map
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 value set to 1
index = 1;
# Return the first value starting
# from 1 which does not exists in map
while (1) :
if (index not in mp.keys()) :
return index;
index += 1;
# Driver code
if __name__ == "__main__" :
a = [ 1, 1, 1, 0, -1, -2 ];
size = len(a);
print("Smallest positive missing number is : ",missingNumber(a, size));
# This code is contributed by AnkitRai01
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 missingNumber(int []a, int n)
{
// Declaring an unordered_map
Dictionary<int,int> m = new Dictionary<int,int>();
// if array value is positive
// store it in map
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);
}
}
}
// index value set to 1
int index = 1;
// Return the first value starting
// from 1 which does not exists in map
while (true)
{
if (!m.ContainsKey(index))
{
return index;
}
index++;
}
}
// Driver code
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));
}
}
// This code is contributed by Princi Singh
JavaScript
<script>
// JavaScript program to find the smallest
// positive missing number
// Function to find the smallest positive
// missing number
function missingNumber(a, n) {
// Declaring an unordered_map
let mp = new Map();
// if array value is positive
// store it in 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)
}
}
}
// index value set to 1
let index = 1;
// Return the first value starting
// from 1 which does not exists in map
while (1) {
if (!mp.has(index)) {
return index;
}
index++;
}
}
// Driver code
let a = [1, 1, 1, 0, -1, -2];
let size = a.length;
document.write("Smallest positive missing number is : " +
missingNumber(a, size) + "<br>");
// This code is contributed by gfgking
</script>
Output: Smallest positive missing number is : 2
Time Complexity: O(N)
Auxiliary Space: O(N)
Similar Reads
Find Smallest Missing Positive Number by Marking Indices Given an unsorted array arr[] with both positive and negative elements, the task is to find the smallest positive number missing from the array.Note: You can modify the original array.Examples:Input: arr[] = {2, -3, 4, 1, 1, 7}Output: 3Explanation: 3 is the smallest positive number missing from the
7 min read
Find largest positive integer x missing from unsorted array such that min(arr[]) < x < max(arr[]) Given an array arr[] containing integers. The task is to find the largest positive integer x missing from the array such that min(arr[]) < x < max(arr[]). Examples Input: arr[] = {2, 3, 7, 6, 8}Output: 5Explanation: 5 is the largest positive integer missing from arr[] and 2 < 5 < 8. Inpu
9 min read
Kth Missing Positive Number in a Sorted Array Given a sorted array of distinct positive integers arr[] and integer k, the task is to find the kth positive number that is missing from arr[].Examples : Input: arr[] = [2, 3, 4, 7, 11], k = 5Output: 9Explanation: Missing are 1, 5, 6, 8, 9, 10, ... and 5th missing number is 9.Input: arr[] = [1, 2, 3
10 min read
Find the missing number in a sorted array of limited range Given a sorted array of size n, consisting of integers from 1 to n+1 with one missing. The task is to find the missing element.Examples: Input : arr[] = [1, 3, 4, 5, 6]Output : 2Input : arr[] = [1, 2, 3, 4, 5, 7, 8, 9, 10]Output : 6Input: arr[] = [1, 2, 3, 4]Output: 5Using Linear Search - O(n) time
11 min read
Minimum operations for which all integers from [0, N] appears as smallest positive missing number (MEX) Given an array arr[], of size N the task is to find the minimum operations on the array such that in each operation any element in the array can be chosen and incremented by 1 so that the MEX is i for all i in the range [0, n]. If for any i if the MEX is not i print -1. Examples : Input : arr[] = {3
13 min read