Related Articles

# Smallest prime number missing in an array

• Difficulty Level : Basic
• Last Updated : 21 May, 2021

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 ``using` `namespace` `std;``#define ll long long int` `// this store all prime number``// upto 10^5``// this function find all prime``vector findPrime(``int` `MAX)``{``    ``bool` `pm[MAX + 1];``    ``memset``(pm, ``true``, ``sizeof``(pm));` `    ``// use sieve to find prime``    ``pm = pm = ``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 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 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);` `    ``// 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 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 prime = ``new` `Vector();``        ``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 prime = findPrime(MAX);` `        ``// now store all number as index of freq arr``        ``// so that we can improve searching``        ``Set s = ``new` `HashSet();``        ``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 = pm = ``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;` `        ``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

 ``
Output:
`5`

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 experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up