Count all Quadruples from four arrays such that their XOR equals to ‘x’
Given four arrays and an integer x, find the number of quadruples which satisfy a^b^c^d = x, where a belongs from Arr1, b belongs from Arr2, c belongs from Arr3, d belongs from Arr4.
Examples :
Input : x = 0;
a[] = { 1 , 10 };
b[] = { 1 , 10 };
c[] = { 1 , 10 };
d[] = { 1 , 10 };
Output : 4
Explanation: There are total 8 Quadruples
with XOR value equals to 0.
{1, 1, 1, 1}, {10, 10, 10, 10}, {1, 1, 10, 10},
{10, 10, 1, 1}, {10, 1, 10, 1}, {1, 10, 1, 10},
{1, 10, 10, 1}, {10, 1, 1, 10}
Input : x = 3
a[] = {0, 1}
b[] = {2, 0}
c[] = {0, 1}
d[] = {0, 1}
Output : 4
Explanation: There are total 4 Quadruples
with XOR value equals to 3.
{0, 2, 0, 1}, {1, 2, 0, 0}, {0, 2, 1, 0},
{1, 2, 1, 1}
Method 1(Naive approach): It can be done using 4 loops, covering every quadruple and checking whether it is equal to x or not.
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
int findQuadruples( int a[], int b[], int c[], int d[],
int x, int n)
{
int count = 0;
for ( int i = 0 ; i < n ; i++)
for ( int j = 0 ; j < n ; j++)
for ( int k = 0 ; k < n ; k++)
for ( int l = 0 ; l < n ; l++)
if ((a[i] ^ b[j] ^ c[k] ^ d[l]) == x)
count++;
return count;
}
int main()
{
int x = 3;
int a[] = {0, 1};
int b[] = {2, 0};
int c[] = {0, 1};
int d[] = {0, 1};
int n = sizeof (a)/ sizeof (a[0]);
cout << findQuadruples(a, b, c, d, x, n) << endl;
return 0;
}
|
Java
class GFG {
static int findQuadruples( int a[], int b[], int c[],
int d[], int x, int n)
{
int count = 0 ;
for ( int i = 0 ; i < n ; i++)
for ( int j = 0 ; j < n ; j++)
for ( int k = 0 ; k < n ; k++)
for ( int l = 0 ; l < n ; l++)
if ((a[i] ^ b[j] ^ c[k] ^ d[l]) == x)
count++;
return count;
}
public static void main(String[] args)
{
int x = 3 ;
int a[] = { 0 , 1 };
int b[] = { 2 , 0 };
int c[] = { 0 , 1 };
int d[] = { 0 , 1 };
int n = a.length;
System.out.println(findQuadruples(a, b, c, d, x, n));
}
}
|
Python3
def findQuadruples(a, b, c, d, x, n):
count = 0
for i in range (n):
for j in range (n):
for k in range (n):
for l in range (n):
if ((a[i] ^ b[j] ^ c[k] ^ d[l]) = = x):
count + = 1
return count
x = 3
a = [ 0 , 1 ]
b = [ 2 , 0 ]
c = [ 0 , 1 ]
d = [ 0 , 1 ]
n = len (a)
print (findQuadruples(a, b, c, d, x, n))
|
C#
using System;
class GFG {
static int findQuadruples( int []a, int []b, int []c,
int []d, int x, int n)
{
int count = 0;
for ( int i = 0; i < n; i++)
for ( int j = 0; j < n; j++)
for ( int k = 0; k < n; k++)
for ( int l = 0; l < n; l++)
if ((a[i] ^ b[j] ^ c[k] ^ d[l]) == x)
count++;
return count;
}
public static void Main()
{
int x = 3;
int []a = {0, 1};
int []b = {2, 0};
int []c = {0, 1};
int []d = {0, 1};
int n = a.Length;
Console.Write(findQuadruples(a, b, c, d, x, n));
}
}
|
PHP
<?php
function findQuadruples( $a , $b , $c ,
$d , $x , $n )
{
$count = 0;
for ( $i = 0 ; $i < $n ; $i ++)
for ( $j = 0 ; $j < $n ; $j ++)
for ( $k = 0 ; $k < $n ; $k ++)
for ( $l = 0 ; $l < $n ; $l ++)
if (( $a [ $i ] ^ $b [ $j ] ^
$c [ $k ] ^ $d [ $l ]) == $x )
$count ++;
return $count ;
}
$x = 3;
$a = array (0, 1);
$b = array (2, 0);
$c = array (0, 1);
$d = array (0, 1);
$n = count ( $a );
echo findQuadruples( $a , $b , $c , $d , $x , $n ) ;
?>
|
Javascript
<script>
function findQuadruples(a, b, c, d, x, n)
{
let count = 0;
for (let i = 0 ; i < n ; i++)
for (let j = 0 ; j < n ; j++)
for (let k = 0 ; k < n ; k++)
for (let l = 0 ; l < n ; l++)
if ((a[i] ^ b[j] ^ c[k] ^ d[l]) == x)
count++;
return count;
}
let x = 3;
let a = [0, 1];
let b = [2, 0];
let c = [0, 1];
let d = [0, 1];
let n = a.length;
document.write(findQuadruples(a, b, c, d, x, n));
</script>
|
Time Complexity: O(n4)
Auxiliary Space: O(1)
Method 2 (Efficient Approach):
The idea is to use meet in the middle algorithm.
For this, observe the pattern below:
a ^ b ^ c ^ d = x
XOR c and d both sides
a ^ b ^ c ^ d ^ c ^ d = x ^ c ^ d
Since, c ^ c = 0 and d ^ d = 0
a ^ b ^ 0 ^ 0 = x ^ c ^ d
That is, a ^ b = x ^ c ^ d
Now, we just have to compute a ^ b and x ^ c ^ d which can be computed in O(n2) each and then find elements by using binary search.
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
int findQuadruples( int a[], int b[], int c[], int d[],
int x, int n)
{
int count = 0;
vector< int > v1, v2;
for ( int i = 0 ; i < n ; i++)
{
for ( int j = 0 ; j < n ; j++)
{
v1.push_back(a[i]^b[j]);
v2.push_back(x ^ c[i] ^ d[j]);
}
}
sort(v1.begin(), v1.end());
for ( int i = 0 ; i < v2.size() ; i++)
{
auto low = lower_bound(v1.begin(), v1.end(), v2[i]);
auto high = upper_bound(v1.begin(), v1.end(), v2[i]);
count += high - low;
}
return count;
}
int main()
{
int x = 3;
int a[] = {0, 1};
int b[] = {2, 0};
int c[] = {0, 1};
int d[] = {0, 1};
int n = sizeof (a)/ sizeof (a[0]);
cout << findQuadruples(a, b, c, d, x, n) << endl;
return 0;
}
|
Java
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
class GFG
{
public static int findQuadruples( int a[], int b[],
int c[], int d[],
int x, int n)
{
int count = 0 ;
ArrayList<Integer> v1 = new ArrayList<>();
ArrayList<Integer> v2 = new ArrayList<>();
for ( int i = 0 ; i < n; i++)
{
for ( int j = 0 ; j < n; j++)
{
v1.add(a[i] ^ b[j]);
v2.add(x ^ c[i] ^ d[j]);
}
}
Collections.sort(v1);
for ( int i = 0 ; i < v2.size(); i++)
{
int low
= Collections.binarySearch(v1, v2.get(i));
int j = low;
for (j = low; j >= 0 ; j--)
{
if (v1.get(j) != v2.get(i))
{
j++;
break ;
}
}
low = j;
int high = Collections.binarySearch(v1, v2.get(i));
j = high;
for (j = high; j < v1.size(); j++)
{
if (v1.get(j) != v2.get(i)) {
break ;
}
}
high = j;
count += high - low;
}
return count;
}
public static void main(String[] args)
{
int x = 3 ;
int a[] = { 0 , 1 };
int b[] = { 2 , 0 };
int c[] = { 0 , 1 };
int d[] = { 0 , 1 };
int n = 2 ;
System.out.println(
findQuadruples(a, b, c, d, x, n));
}
}
|
Python3
from bisect import bisect_left, bisect_right
def findQuadruples(a, b, c, d, x, n):
count = 0
v1, v2 = [], []
for i in range (n):
for j in range (n):
v1.append(a[i] ^ b[j])
v2.append(x ^ c[i] ^ d[j])
v1 = sorted (v1)
for i in range ( len (v2)):
low = bisect_left(v1, v2[i])
high = bisect_right(v1, v2[i])
count + = high - low
return count
if __name__ = = '__main__' :
x = 3
a = [ 0 , 1 ]
b = [ 2 , 0 ]
c = [ 0 , 1 ]
d = [ 0 , 1 ]
n = len (a)
print (findQuadruples(a, b, c, d, x, n))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int findQuadruples( int [] a, int [] b,
int [] c, int [] d,
int x, int n)
{
int count = 0;
List< int > v1 = new List< int >();
List< int > v2 = new List< int >();
for ( int i = 0 ; i < n ; i++)
{
for ( int j = 0 ; j < n ; j++)
{
v1.Add(a[i]^b[j]);
v2.Add(x ^ c[i] ^ d[j]);
}
}
v1.Sort();
for ( int i = 0 ; i < v2.Count; i++)
{
int low = v1.BinarySearch(v2[i]);
int j = low;
for (j = low; j >= 0; j--)
{
if (v1[j] != v2[i])
{
j++;
break ;
}
}
low = j;
int high = v1.BinarySearch(v2[i]);
j = high;
for (j = high; j < v1.Count; j++)
{
if (v1[j] != v2[i])
{
break ;
}
}
high = j;
count += high - low;
}
return count;
}
static void Main()
{
int x = 3;
int [] a = {0, 1};
int [] b = {2, 0};
int [] c = {0, 1};
int [] d = {0, 1};
int n = a.Length;
Console.WriteLine(findQuadruples(a, b, c, d, x, n));
}
}
|
Javascript
<script>
function findQuadruples(a, b, c, d, x, n)
{
let count = 0;
let v1 = [];
let v2 = [];
for (let i = 0; i < n; i++)
{
for (let j = 0; j < n; j++)
{
v1.push(a[i] ^ b[j]);
v2.push(x ^ c[i] ^ d[j]);
}
}
v1.sort();
for (let i = 0; i < v2.length; i++)
{
let low = 0;
for (let z = 0; z < v1.length; z++)
{
if (v1[z] == v2[i])
{
low = z;
break ;
}
}
let j = low;
for (j = low; j >= 0; j--)
{
if (v1[j] != v2[i])
{
j++;
break ;
}
}
low = j;
let high = 0;
for (let z = 0; z < v1.length; z++)
{
if (v1[z] == v2[i])
{
high = z;
break ;
}
}
j = high;
for (j = high; j < v1.length; j++)
{
if (v1[j] != v2[i]) {
break ;
}
}
high = j;
count += high - low;
}
return count;
}
let x = 3;
let a = [ 0, 1 ];
let b = [ 2, 0 ];
let c = [ 0, 1 ];
let d = [ 0, 1 ];
let n = 2;
document.write(
findQuadruples(a, b, c, d, x, n));
</script>
|
Time Complexity: O(n2log(n))
Auxiliary Space: O(n2)
Last Updated :
13 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...