Count Pairs with XOR Representable as Sum of Same Parity Primes
Last Updated :
29 Nov, 2023
Given an array A[] of length N, the task is to count the number of distinct pairs of indices (i, j), such that, (A[i] XOR A[j]) can be expressed as the sum of two prime numbers of the same parity.
Examples:
Input: N = 5, A[] = {2, 4, 8, 1, 3}
Output: 3
Explanation: There are total three such pairs:
- (0, 1) => A[0] XOR A[1] = 2 XOR 4 = 6 and 6 can be expressed as sum of two prime numbers of same parity (3 + 3)
- (0, 2) => A[0] XOR A[2] = 2 XOR 8 = 10 and 10 can be expressed as sum of two prime numbers of same parity (3 + 7)
- (1, 2) => A[1] XOR A[2] = 4 XOR 8 = 12 and 12 can be expressed as sum of two prime numbers of same parity (5 + 7).
Input: N = 3, A[] = {1, 1, 3}
Output: 0
Explanation: It can be verified that there are no pairs following the given conditions. Therefore, output is 0.
Approach: Implement the idea below to solve the problem
Observations:
- The sum of any two numbers of the same parity is an even Number. Therefore, the XOR should be an even number.
- Among all the even numbers, 0 and 2 cannot be represented as the sum of two prime numbers.
- All even numbers greater than 2 can be represented as the sum of two prime numbers (Goldbach’s Conjecture)
From the above observations, we simply need to find number of pairs (i, j) such that their XOR is an Even number greater than 2. The XOR of two numbers is even only if both the numbers are of same parity. So, we can count all the pairs having Even XOR by pairing all the Odd numbers among themselves and all the Even numbers among themselves. Finally, we need to subtract those pairs whose XOR is 0 or 2.
- For XOR = 0, both the numbers should be same so we can count the frequency of all the elements in a frequency array and calculate the count of such pairs.
- For XOR = 2, we can simply use the frequency array to count such pairs.
Steps to solve the problem:
- Create two variables even, odd to count odd and even elements.
- Declare a map let say freq_arr for counting frequencies of elements.
- Run a loop for initializing map, odd and even.
- Initialize a variable ans = (even * (even – 1))/2 + (odd * (odd – 1))/2, to store the total number of pairs whose XOR is Even.
- Iterate over freq_arr
- Subtract the number of pairs whose XOR is 0 from ans
- Subtract the number of pairs whose XOR is 2 from ans
- Replace the frequency array of current element as 0, to prevent counting them twice (while counting pairs whose XOR is 2)
- Return ans as the final answer
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void CountPairs( int N, vector< int >& A)
{
int even = 0, odd = 0, ans = 0;
unordered_map< int , int > freq_arr;
for ( int i = 0; i < N; i++) {
freq_arr[A[i]]++;
if (A[i] % 2 == 1)
odd++;
else
even++;
}
ans = (even * (even - 1)) / 2 + (odd * (odd - 1)) / 2;
for ( const auto & i : freq_arr) {
ans -= (i.second * (i.second - 1)) / 2;
ans -= i.second * freq_arr[i.first ^ 2];
freq_arr[i.first] = 0;
}
cout << ans << endl;
}
int main()
{
int N = 5;
vector< int > A = { 2, 4, 8, 1, 3 };
CountPairs(N, A);
return 0;
}
|
Java
import java.util.*;
public class GFG {
public static void main(String[] args)
{
Integer N = 5 ;
Integer[] A = { 2 , 4 , 8 , 1 , 3 };
Count_Pairs(N, A);
}
public static void Count_Pairs(Integer N, Integer[] A)
{
Integer even = 0 , odd = 0 , ans = 0 ;
Map<Integer, Integer> freq_arr = new HashMap<>();
for (Integer i = 0 ; i < N; i++) {
freq_arr.put(
A[i], freq_arr.getOrDefault(A[i], 0 ) + 1 );
if ((A[i] & 1 ) == 1 )
odd++;
else
even++;
}
ans = (even * (even - 1 )) / 2
+ (odd * (odd - 1 )) / 2 ;
for (Map.Entry<Integer, Integer> i :
freq_arr.entrySet()) {
ans -= (i.getValue() * (i.getValue() - 1 )) / 2 ;
ans -= (i.getValue()
* freq_arr.getOrDefault(i.getKey() ^ 2 ,
0 ));
freq_arr.put(i.getKey(), 0 );
}
System.out.println(ans);
}
}
|
Python3
def CountPairs(N, A):
even = 0
odd = 0
ans = 0
freq_arr = {}
for i in range (N):
freq_arr[A[i]] = freq_arr.get(A[i], 0 ) + 1
if A[i] % 2 = = 1 :
odd + = 1
else :
even + = 1
ans = (even * (even - 1 )) / 2 + (odd * (odd - 1 )) / 2
for key, value in freq_arr.items():
ans - = (value * (value - 1 )) / 2
ans - = value * freq_arr.get(key ^ 2 , 0 )
freq_arr[key] = 0
return ans
if __name__ = = '__main__' :
N = 5
A = [ 2 , 4 , 8 , 1 , 3 ]
ans = CountPairs(N, A)
print (ans)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static double CountPairs( int N, List< int > A)
{
int even = 0;
int odd = 0;
double ans = 0;
Dictionary< int , int > freqArr = new Dictionary< int , int >();
for ( int i = 0; i < N; i++)
{
freqArr[A[i]] = freqArr.TryGetValue(A[i], out int value) ? value + 1 : 1;
if (A[i] % 2 == 1)
odd++;
else
even++;
}
ans = (even * (even - 1)) / 2 + (odd * (odd - 1)) / 2;
foreach ( var key in new List< int >(freqArr.Keys))
{
ans -= (freqArr[key] * (freqArr[key] - 1)) / 2;
ans -= freqArr[key] * freqArr.GetValueOrDefault(key ^ 2, 0);
freqArr[key] = 0;
}
return ans;
}
static void Main()
{
int N = 5;
List< int > A = new List< int > { 2, 4, 8, 1, 3 };
double ans = CountPairs(N, A);
Console.WriteLine(ans);
}
}
|
Javascript
function countPairs(N, A) {
let even = 0, odd = 0, ans = 0;
const freqArr = new Map();
for (let i = 0; i < N; i++) {
if (!freqArr.has(A[i])) {
freqArr.set(A[i], 1);
} else {
freqArr.set(A[i], freqArr.get(A[i]) + 1);
}
if (A[i] % 2 === 1) {
odd++;
} else {
even++;
}
}
ans = (even * (even - 1)) / 2 + (odd * (odd - 1)) / 2;
for (const [key, value] of freqArr) {
ans -= (value * (value - 1)) / 2;
ans -= value * (freqArr.get(key ^ 2) || 0);
freqArr.set(key, 0);
}
console.log(ans);
}
const N = 5;
const A = [2, 4, 8, 1, 3];
countPairs(N, A);
|
Time Complexity: O(N), where N is the size of input array A[]
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...