All pairs whose xor gives unique prime | Java 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
import java . util . * ; class solution { static boolean 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 ; } static int countPairs ( int a [ ] , int n ) { int count = 0 ; Map < Integer , Integer > m = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( isPrime ( a [ i ] ^ a [ j ] ) && m . get ( a [ i ] ^ a [ j ] ) == null ) { m . put ( ( a [ i ] ^ a [ j ] ) , 1 ) ; count ++ ; } } } return count ; } public static void main ( String args [ ] ) { int a [ ] = { 10 , 12 , 23 , 45 , 5 , 6 } ; int n = a . length ; System . out . println ( countPairs ( a , n ) ) ; } }