All pairs whose xor gives unique prime

Given an array arr[], the task is to count all the pairs whose xor gives the unique prime, i.e. no two pairs should give the same prime.

Examples:

Input: arr[] = {2, 3, 4, 5, 6, 7, 8, 9}
Output: 6
(2, 5), (2, 7), (2, 9), (4, 6), (4, 7) and (4, 9) are the only pairs whose XORs are primes i.e. 7, 5, 11, 2, 3 and 13 respectively.

Input: arr[] = {10, 12, 23, 45, 5, 6}
Output: 4

Approach: Iterating every possible pair and check whether the xor of the current pair is a prime. If its a prime then update count = count + 1 and also save the prime in an unordered_map in order to keep track of the repeating primes. Print the count in the end.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function that returns true if n is prime
bool isPrime(int n)
{
    // Corner cases
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;
  
    // This is checked so that we can skip
    // middle five numbers in below loop
    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;
}
  
// Function to return the count of valid pairs
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 xor(a[i], a[j]) is prime and unique
            if (isPrime(a[i] ^ a[j]) && m[a[i] ^ a[j]] == 0) {
                m[(a[i] ^ a[j])]++;
                count++;
            }
        }
    }
  
    return count;
}
  
// Driver code
int main()
{
    int a[] = { 10, 12, 23, 45, 5, 6 };
  
    int n = sizeof(a) / sizeof(a[0]);
  
    cout << countPairs(a, n);
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of above approach
import java.util.*;
class solution
{
   
// Function that returns true if n is prime
static boolean isPrime(int n)
{
    // Corner cases
    if (n <= 1)
        return false;
    if (n <= 3)
        return true;
   
    // This is checked so that we can skip
    // middle five numbers in below loop
    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;
}
   
// Function to return the count of valid pairs
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 xor(a[i], a[j]) is prime and unique
            if (isPrime(a[i] ^ a[j]) && m.get(a[i] ^ a[j]) == null) {
                m.put((a[i] ^ a[j]),1);
                count++;
            }
        }
    }
   
    return count;
}
   
// Driver code
public static void main(String args[])
{
    int a[] = { 10, 12, 23, 45, 5, 6 };
   
    int n = a.length;
   
    System.out.println(countPairs(a, n));
}
}
//contributed by Arnab Kundu

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach 
using System ;
using System.Collections ;
  
class solution 
  
    // Function that returns true if n is prime 
    static bool isPrime(int n) 
    
        // Corner cases 
        if (n <= 1) 
            return false
        if (n <= 3) 
            return true
      
        // This is checked so that we can skip 
        // middle five numbers in below loop 
        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
    
      
    // Function to return the count of valid pairs 
    static int countPairs(int []a, int n) 
    
        int count = 0; 
      
        Hashtable m=new Hashtable(); 
      
        for (int i = 0; i < n - 1; i++) { 
            for (int j = i + 1; j < n; j++) { 
      
                // If xor(a[i], a[j]) is prime and unique 
                if (isPrime(a[i] ^ a[j]) && m[a[i] ^ a[j]] == null)
                
                    m.Add((a[i] ^ a[j]),1); 
                    count++; 
                
            
        
      
        return count; 
    
      
    // Driver code 
    public static void Main() 
    
        int []a = { 10, 12, 23, 45, 5, 6 }; 
      
        int n = a.Length; 
      
        Console.WriteLine(countPairs(a, n)); 
    
    // This code is contributed by Ryuga

chevron_right


Output:

4


My Personal Notes arrow_drop_up