Print all possible pair with prime XOR in the Array
Given an array arr[] of N positive integers. The task is to print all possible pairs such that their XOR is a Prime Number.
Examples:
Input: arr[] = {1, 3, 6, 11}
Output: (1, 3) (1, 6) (3, 6) (6, 11)
Explanation:
The XOR of the above pairs:
1^3 = 2
1^6 = 7
3^6 = 5
6^11 = 13
Input: arr[] = { 22, 58, 63, 0, 47 }
Output: (22, 63) (58, 63) (0, 47)
Explanation:
The XOR of the above pairs:
22^33 = 37
58^63 = 5
0^47 = 47
Approach:
- Generate all Prime Numbers using Sieve of Eratosthenes.
- For all possible pairs from the given array, check if the XOR of that pair is prime or not.
- If the XOR of a pair is prime then print that pair else check for the next pair.
Below is the implementation of the above approach:
CPP
#include <bits/stdc++.h>
using namespace std;
const int sz = 1e5;
bool isPrime[sz + 1];
void generatePrime()
{
int i, j;
memset (isPrime, true , sizeof (isPrime));
isPrime[0] = isPrime[1] = false ;
for (i = 2; i * i <= sz; i++) {
if (isPrime[i]) {
for (j = i * i; j < sz; j += i) {
isPrime[j] = false ;
}
}
}
}
void Pair_of_PrimeXor( int A[], int n)
{
for ( int i = 0; i < n; i++) {
for ( int j = i + 1; j < n; j++) {
if (isPrime[(A[i] ^ A[j])]) {
cout << "(" << A[i]
<< ", " << A[j] << ") " ;
}
}
}
}
int main()
{
int A[] = { 1, 3, 6, 11 };
int n = sizeof (A) / sizeof (A[0]);
generatePrime();
Pair_of_PrimeXor(A, n);
return 0;
}
|
Java
class GFG
{
static int sz = ( int ) 1e5;
static boolean []isPrime = new boolean [sz + 1 ];
static void generatePrime()
{
int i, j;
for (i = 2 ; i <= sz; i++)
isPrime[i] = true ;
for (i = 2 ; i * i <= sz; i++) {
if (isPrime[i]) {
for (j = i * i; j < sz; j += i) {
isPrime[j] = false ;
}
}
}
}
static void Pair_of_PrimeXor( int A[], int n)
{
for ( int i = 0 ; i < n; i++) {
for ( int j = i + 1 ; j < n; j++) {
if (isPrime[(A[i] ^ A[j])]) {
System.out.print( "(" + A[i]
+ ", " + A[j]+ ") " );
}
}
}
}
public static void main(String[] args)
{
int A[] = { 1 , 3 , 6 , 11 };
int n = A.length;
generatePrime();
Pair_of_PrimeXor(A, n);
}
}
|
Python
sz = 10 * * 5
isPrime = [ True ] * (sz + 1 )
def generatePrime():
i, j = 0 , 0
isPrime[ 0 ] = isPrime[ 1 ] = False
for i in range ( 2 , sz + 1 ):
if i * i > sz:
break
if (isPrime[i]):
for j in range (i * i, sz, i):
isPrime[j] = False
def Pair_of_PrimeXor(A, n):
for i in range (n):
for j in range (i + 1 , n):
if (isPrime[(A[i] ^ A[j])]):
print ( "(" ,A[i], "," ,A[j], ")" ,end = " " )
if __name__ = = '__main__' :
A = [ 1 , 3 , 6 , 11 ]
n = len (A)
generatePrime()
Pair_of_PrimeXor(A, n)
|
C#
using System;
class GFG
{
static int sz = ( int ) 1e5;
static bool []isPrime = new bool [sz + 1];
static void generatePrime()
{
int i, j;
for (i = 2; i <= sz; i++)
isPrime[i] = true ;
for (i = 2; i * i <= sz; i++) {
if (isPrime[i]) {
for (j = i * i; j < sz; j += i) {
isPrime[j] = false ;
}
}
}
}
static void Pair_of_PrimeXor( int []A, int n)
{
for ( int i = 0; i < n; i++) {
for ( int j = i + 1; j < n; j++) {
if (isPrime[(A[i] ^ A[j])]) {
Console.Write( "(" + A[i]
+ ", " + A[j]+ ") " );
}
}
}
}
public static void Main(String[] args)
{
int []A = { 1, 3, 6, 11 };
int n = A.Length;
generatePrime();
Pair_of_PrimeXor(A, n);
}
}
|
Javascript
<script>
const sz = 100000;
let isPrime = new Array(sz + 1).fill( true );
function generatePrime()
{
let i, j;
isPrime[0] = isPrime[1] = false ;
for (i = 2; i * i <= sz; i++) {
if (isPrime[i]) {
for (j = i * i; j < sz; j += i) {
isPrime[j] = false ;
}
}
}
}
function Pair_of_PrimeXor(A, n)
{
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
if (isPrime[(A[i] ^ A[j])]) {
document.write( "(" + A[i]
+ ", " + A[j] + ") " );
}
}
}
}
let A = [ 1, 3, 6, 11 ];
let n = A.length;
generatePrime();
Pair_of_PrimeXor(A, n);
</script>
|
Output:
(1, 3) (1, 6) (3, 6) (6, 11)
Time Complexity: O(N2), where N is the length of the given array.
Auxiliary Space: O(sz)
Last Updated :
28 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...