All pairs whose xor gives unique prime | Function that returns true if n is prime ; Corner cases ; This is checked so that we can skip middle five numbers in below loop ; Function to return the count of valid pairs ; If xor ( a [ i ] , a [ j ] ) is prime and unique ; Driver code
function isPrime ( n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( let i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } function countPairs ( a , n ) { let count = 0 ; let m = new Map ( ) ; for ( let i = 0 ; i < n - 1 ; i ++ ) { for ( let j = i + 1 ; j < n ; j ++ ) { if ( isPrime ( a [ i ] ^ a [ j ] ) && ! m . has ( a [ i ] ^ a [ j ] ) ) { m . set ( ( a [ i ] ^ a [ j ] ) , 1 ) ; count ++ ; } } } return count ; } let a = [ 10 , 12 , 23 , 45 , 5 , 6 ] ; let n = a . length ; document . write ( countPairs ( a , n ) ) ;