All pairs whose xor gives unique prime | C ++ implementation of above approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; bool isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } int countPairs ( int a [ ] , int n ) { int count = 0 ; unordered_map < int , int > m ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( isPrime ( a [ i ] ^ a [ j ] ) && m [ a [ i ] ^ a [ j ] ] == 0 ) { m [ ( a [ i ] ^ a [ j ] ) ] ++ ; count ++ ; } } } return count ; } int main ( ) { int a [ ] = { 10 , 12 , 23 , 45 , 5 , 6 } ; int n = sizeof ( a ) / sizeof ( a [ 0 ] ) ; cout << countPairs ( a , n ) ; }