Count Sexy Prime Pairs in the given array | To store check the prime number ; A utility function that find the Prime Numbers till N ; Resize the Prime Number ] ; Loop till sqrt ( N ) to find prime numbers and make their multiple false in the bool array Prime ; Function that returns the count of SPP ( Sexy Prime Pair ) Pairs ; Find the maximum element in the given array arr [ ] ; Function to calculate the prime numbers till N ; To store the count of pairs ; To store the frequency of element in the array arr [ ] ; Sort before traversing the array ; Traverse the array and find the pairs with SPP ( Sexy Prime Pair ) ; If current element is Prime , then check for ( current element + 6 ) ; Return the count of pairs ; Driver code ; Function call to find SPP ( Sexy Prime Pair ) pair
var Prime = Array ( 100 ) . fill ( true ) ; function computePrime ( N ) { var i , j ; Prime [ 0 ] = Prime [ 1 ] = false ; for ( i = 2 ; i * i <= N ; i ++ ) { if ( Prime [ i ] ) { for ( j = i * i ; j < N ; j += i ) { Prime [ j ] = false ; } } } } function countSexyPairs ( arr , n ) { var maxE = Math . max . apply ( Math , arr ) ; computePrime ( maxE ) ; var count = 0 ; var freq = Array ( maxE + 1 ) . fill ( 0 ) ; for ( i = 0 ; i < n ; i ++ ) { freq [ arr [ i ] ] ++ ; } arr . sort ( ) ; for ( i = 0 ; i < n ; i ++ ) { if ( Prime [ arr [ i ] ] ) { if ( freq [ arr [ i ] + 6 ] > 0 && Prime [ arr [ i ] + 6 ] ) { count ++ ; } } } return count ; } var arr = [ 6 , 7 , 5 , 11 , 13 ] ; var n = arr . length ; document . write ( countSexyPairs ( arr , n ) ) ;