Smallest prime number missing in an array | C # implementation of the above approach ; this store all prime number upto 10 ^ 5 this function find all prime ; use sieve to find prime ; if number is prime then store it in prime vector ; Function to find the smallest prime missing ; first of all find all prime ; now store all number as index of freq arr so that we can improve searching ; now check for each prime ; Driver code ; find smallest prime which is not present
using System ; using System . Collections ; using System . Collections . Generic ; class GFG { static ArrayList findPrime ( int MAX ) { bool [ ] pm = new bool [ MAX + 1 ] ; for ( int i = 0 ; i < MAX + 1 ; i ++ ) pm [ i ] = true ; 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 ; 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 ; } static int findSmallest ( int [ ] arr , int n ) { int MAX = max_element ( arr ) ; ArrayList prime = findPrime ( MAX ) ; HashSet < int > s = new HashSet < int > ( ) ; for ( int i = 0 ; i < arr . Length ; i ++ ) s . Add ( arr [ i ] ) ; 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 ; } static void Main ( ) { int [ ] arr = { 3 , 0 , 1 , 2 , 7 } ; int n = arr . Length ; if ( findSmallest ( arr , n ) == - 1 ) Console . Write ( " No ▁ prime ▁ number ▁ missing " ) ; else Console . Write ( findSmallest ( arr , n ) ) ; } }