Smallest prime number missing in an array
Last Updated :
08 Sep, 2022
Given an array containing n distinct numbers. The task is to find the smallest prime which is not present in the array.
Note: If there is no prime number missing up to the maximum element of the array then print "No prime number missing".
Examples:
Input: arr[] = {9, 11, 4, 2, 3, 7, 0, 1}
Output: 5
5 is the smallest prime, which is not present in array.
Input: arr[] = {3, 0, 2, 5}
Output: No prime number missing
As 5 is the maximum element and all prime numbers upto 5
are present in the array.
Approach: First of all, find all prime numbers using Sieve of Eratosthenes then sequentially check which one is not present there. Just iterate over the array and check whether the number is there or not using hashing.
Below is the implementation of the above approach:
C++
// C++ implementation of the above approach
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
// this store all prime number
// upto 10^5
// this function find all prime
vector<ll> findPrime(int MAX)
{
bool pm[MAX + 1];
memset(pm, true, sizeof(pm));
// use sieve to find prime
pm[0] = pm[1] = false;
for (int i = 2; i <= MAX; i++)
if (pm[i])
for (int j = 2 * i; j <= MAX; j += i)
pm[j] = false;
// if number is prime then
// store it in prime vector
vector<ll> prime;
for (int i = 0; i <= MAX; i++)
if (pm[i])
prime.push_back(i);
return prime;
}
// Function to find the smallest prime missing
int findSmallest(int arr[], int n)
{
int MAX = *max_element(arr, arr + n);
// first of all find all prime
vector<ll> prime = findPrime(MAX);
// now store all number as index of freq arr
// so that we can improve searching
unordered_set<int> s;
for (int i = 0; i < n; i++)
s.insert(arr[i]);
// now check for each prime
int ans = -1;
for (int i = 0; i < prime.size(); i++)
if (s.find(prime[i]) == s.end()) {
ans = prime[i];
break;
}
return ans;
}
// Driver code
int main()
{
int arr[] = { 3, 0, 1, 2, 7 };
int n = sizeof(arr) / sizeof(arr[0]);
// find smallest prime
// which is not present
if (findSmallest(arr, n) == -1)
cout << "No prime number missing";
else
cout << findSmallest(arr, n);
return 0;
}
Java
// Java implementation of the above approach
import java.util.*;
class GFG {
// this store all prime number
// upto 10^5
// this function find all prime
static Vector<Integer> findPrime(int MAX)
{
boolean pm[] = new boolean[MAX + 1];
for (int i = 0; i < pm.length; i++)
pm[i] = true;
// use sieve to find prime
pm[0] = pm[1] = false;
for (int i = 2; i <= MAX; i++)
if (pm[i])
for (int j = 2 * i; j <= MAX; j += i)
pm[j] = false;
// if number is prime then
// store it in prime vector
Vector<Integer> prime = new Vector<Integer>();
for (int i = 0; i <= MAX; i++)
if (pm[i])
prime.add(i);
return prime;
}
static int max_element(int arr[])
{
int max = arr[0];
for (int i = 0; i < arr.length; i++)
max = Math.max(max, arr[i]);
return max;
}
// Function to find the smallest prime missing
static int findSmallest(int arr[], int n)
{
int MAX = max_element(arr);
// first of all find all prime
Vector<Integer> prime = findPrime(MAX);
// now store all number as index of freq arr
// so that we can improve searching
Set<Integer> s = new HashSet<Integer>();
for (int i = 0; i < arr.length; i++)
s.add(arr[i]);
// now check for each prime
long ans = -1;
for (int i = 0; i < prime.size(); i++) {
if (!s.contains(prime.get(i))) {
ans = (prime.get(i));
break;
}
}
return (int)ans;
}
// Driver code
public static void main(String args[])
{
int arr[] = { 3, 0, 1, 2, 7 };
int n = arr.length;
// find smallest prime
// which is not present
if (findSmallest(arr, n) == -1)
System.out.print("No prime number missing");
else
System.out.print(findSmallest(arr, n));
}
}
// This code is contributed by Arnab Kundu
Python3
# Python3 implementation of the above approach
# This function finds all
# prime numbers upto 10 ^ 5
def findPrime(MAX):
pm = [True] * (MAX + 1)
# use sieve to find prime
pm[0], pm[1] = False, False
for i in range(2, MAX + 1):
if pm[i] == True:
for j in range(2 * i, MAX + 1, i):
pm[j] = False
# If number is prime, then
# store it in prime list
prime = []
for i in range(0, MAX + 1):
if pm[i] == True:
prime.append(i)
return prime
# Function to find the smallest prime missing
def findSmallest(arr, n):
MAX = max(arr)
# first of all find all prime
prime = findPrime(MAX)
# now store all number as index of freq
# arr so that we can improve searching
s = set()
for i in range(0, n):
s.add(arr[i])
# now check for each prime
ans = -1
for i in range(0, len(prime)):
if prime[i] not in s:
ans = prime[i]
break
return ans
# Driver Code
if __name__ == "__main__":
arr = [3, 0, 1, 2, 7]
n = len(arr)
# find smallest prime
# which is not present
if(findSmallest(arr, n) == -1):
print("No prime number missing")
else:
print(findSmallest(arr, n))
# This code is contributed by Rituraj Jain
C#
// C# implementation of the above approach
using System;
using System.Collections;
using System.Collections.Generic;
class GFG
{
// this store all prime number
// upto 10^5
// this function find all prime
static ArrayList findPrime(int MAX)
{
bool[] pm = new bool[MAX + 1];
for (int i = 0; i < MAX + 1; i++)
pm[i] = true;
// use sieve to find prime
pm[0] = pm[1] = false;
for (int i = 2; i <= MAX; i++)
if (pm[i])
for (int j = 2 * i; j <= MAX; j += i)
pm[j] = false;
// if number is prime then
// store it in prime vector
ArrayList prime = new ArrayList();
for (int i = 0; i <= MAX; i++)
if (pm[i])
prime.Add(i);
return prime;
}
static int max_element(int []arr)
{
int max = arr[0];
for (int i = 0; i < arr.Length; i++)
max = Math.Max(max, arr[i]);
return max;
}
// Function to find the smallest prime missing
static int findSmallest(int []arr, int n)
{
int MAX = max_element(arr);
// first of all find all prime
ArrayList prime = findPrime(MAX);
// now store all number as index of freq arr
// so that we can improve searching
HashSet<int> s = new HashSet<int>();
for (int i = 0; i < arr.Length; i++)
s.Add(arr[i]);
// now check for each prime
int ans = -1;
for (int i = 0; i < prime.Count; i++)
{
if (!s.Contains((int)prime[i]))
{
ans = (int)(prime[i]);
break;
}
}
return (int)ans;
}
// Driver code
static void Main()
{
int []arr = { 3, 0, 1, 2, 7 };
int n = arr.Length;
// find smallest prime
// which is not present
if (findSmallest(arr, n) == -1)
Console.Write("No prime number missing");
else
Console.Write(findSmallest(arr, n));
}
}
// This code is contributed by mits
JavaScript
<script>
// Javascript implementation of the above approach
// this store all prime number
// upto 10^5
// this function find all prime
function findPrime(MAX)
{
let pm = new Array(MAX + 1);
pm.fill(true);
// use sieve to find prime
pm[0] = pm[1] = false;
for (let i = 2; i <= MAX; i++)
if (pm[i])
for (let j = 2 * i; j <= MAX; j += i)
pm[j] = false;
// if number is prime then
// store it in prime vector
let prime = new Array();
for (let i = 0; i <= MAX; i++)
if (pm[i])
prime.push(i);
return prime;
}
// Function to find the smallest prime missing
function findSmallest(arr, n) {
let MAX = arr.sort((A, B) => A - B).reverse()[0];
// first of all find all prime
let prime = findPrime(MAX);
// now store all number as index of freq arr
// so that we can improve searching
let s = new Set();
for (let i = 0; i < n; i++)
s.add(arr[i]);
// now check for each prime
let ans = -1;
for (let i = 0; i < prime.length; i++){
if (!s.has(prime[i])) {
ans = prime[i];
break;
}
}
return ans;
}
// Driver code
let arr = [3, 0, 1, 2, 7];
let n = arr.length;
// find smallest prime
// which is not present
if (findSmallest(arr, n) == -1)
document.write("No prime number missing");
else
document.write(findSmallest(arr, n));
// This code is contributed by gfgking.
</script>
Complexity Analysis:
- Time Complexity: O(n + MAX2)
- Auxiliary Space: O(n + MAX)
Similar Reads
Find Smallest Missing Positive Number Given an unsorted array arr[] with both positive and negative elements, find the smallest positive number missing from the array.Examples:Input: arr[] = [2, -3, 4, 1, 1, 7]Output: 3Explanation: 3 is the smallest positive number missing from the array.Input: arr[] = [5, 3, 2, 5, 1]Output: 4Explanatio
15+ 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 smallest missing number Given a sorted array of n distinct integers where each integer is in the range from 0 to m-1 and m > n. Find the smallest number that is missing from the array. Examples: Input: {0, 1, 2, 6, 9}, n = 5, m = 10 Output: 3 Input: {4, 5, 10, 11}, n = 4, m = 12 Output: 0 Input: {0, 1, 2, 3}, n = 4, m =
15 min read
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
Javascript Program for Find the smallest missing number Given a sorted array of n distinct integers where each integer is in the range from 0 to m-1 and m > n. Find the smallest number that is missing from the array. Examples:Input: {0, 1, 2, 6, 9}, n = 5, m = 10 Output: 3 Input: {4, 5, 10, 11}, n = 4, m = 12 Output: 0 Input: {0, 1, 2, 3}, n = 4, m =
4 min read