Smallest prime number missing in an array | 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
function findPrime ( MAX ) { let pm = new Array ( MAX + 1 ) ; pm . fill ( true ) ; 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 ; let prime = new Array ( ) ; for ( let i = 0 ; i <= MAX ; i ++ ) if ( pm [ i ] ) prime . push ( i ) ; return prime ; } function findSmallest ( arr , n ) { let MAX = arr . sort ( ( A , B ) => A - B ) . reverse ( ) [ 0 ] ; let prime = findPrime ( MAX ) ; let s = new Set ( ) ; for ( let i = 0 ; i < n ; i ++ ) s . add ( arr [ i ] ) ; let ans = - 1 ; for ( let i = 0 ; i < prime . length ; i ++ ) { if ( ! s . has ( prime [ i ] ) ) { ans = prime [ i ] ; break ; } } return ans ; } let arr = [ 3 , 0 , 1 , 2 , 7 ] ; let n = arr . length ; if ( findSmallest ( arr , n ) == - 1 ) document . write ( " " ) ; else document . write ( findSmallest ( arr , n ) ) ;