# 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}
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Method 1(Naive approach)
It can be done using 4 loops, covering every quadruple and checking whether it is equal to x or not.

## C++

 `// C++ program to find number of Quadruples from four ` `// arrays such that their XOR equals to 'x' ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the number of Quadruples with XOR ` `// equals to x such that every element of Quadruple is ` `// from different array. ` `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++) ` ` `  `                    ``// Check whether XOR is equal to x ` `                    ``if` `((a[i] ^ b[j] ^ c[k] ^ d[l]) == x) ` `                        ``count++; ` ` `  `    ``return` `count; ` `} ` ` `  `// Driver Program ` `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); ` ` `  `    ``cout << findQuadruples(a, b, c, d, x, n) << endl; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to find number of Quadruples from four ` `// arrays such that their XOR equals to 'x' ` `class` `GFG { ` `     `  `    ``// Function to return the number of Quadruples with XOR ` `    ``// equals to x such that every element of Quadruple is ` `    ``// from different array. ` `    ``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++) ` `     `  `                        ``// Check whether XOR is equal to x ` `                        ``if` `((a[i] ^ b[j] ^ c[k] ^ d[l]) == x) ` `                            ``count++; ` `     `  `        ``return` `count; ` `    ``} ` `     `  `    ``// Driver method ` `    ``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)); ` `    ``} ` `} ` ` `  `// This code is contributed by Anant Agarwal. `

## Python3

 `# Python3 program to find number of ` `# Quadruples from four arrays such ` `# that their XOR equals to 'x' ` ` `  `# Function to return the number of ` `# Quadruples with XOR equals to x ` `# such that every element of Quadruple  ` `# is from different array. ` `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): ` ` `  `                    ``# Check whether XOR is equal to x ` `                    ``if` `((a[i] ^ b[j] ^ c[k] ^ d[l]) ``=``=` `x): ` `                        ``count ``+``=` `1` `    ``return` `count ` ` `  `# Driver Code ` `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)) ` ` `  `# This code is contributed by Anant Agarwal. `

## C#

 `// C# program to find number of  ` `// Quadruples from four arrays such ` `// that their XOR equals to 'x' ` `using` `System; ` ` `  `class` `GFG { ` `     `  `    ``// Function to return the number of  ` `    ``// Quadruples with XOR equals to x such that  ` `    ``// every element of Quadruple is from different array. ` `    ``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++) ` `     `  `                        ``// Check whether XOR is equal to x ` `                        ``if` `((a[i] ^ b[j] ^ c[k] ^ d[l]) == x) ` `                            ``count++; ` `     `  `        ``return` `count; ` `    ``} ` `     `  `    ``// Driver method ` `    ``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; ` `         `  `        ``// Function calling ` `        ``Console.Write(findQuadruples(a, b, c, d, x, n)); ` `    ``} ` `} ` ` `  `// This code is contributed by nitin mittal. `

## PHP

 ` `

Output:

`4`

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.

 `// C++ program to find number of Quadruples from four ` `// arrays such that their XOR equals to 'x' ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the number of Quadruples with XOR ` `// equals to x such that every element of Quadruple is ` `// from different array. ` `int` `findQuadruples(``int` `a[], ``int` `b[], ``int` `c[], ``int` `d[], ` `                   ``int` `x, ``int` `n) ` `{ ` `    ``int` `count = 0; ` `    ``vector<``int``> v1, v2; ` ` `  `    ``// Loop to get two different subsets ` `    ``for` `(``int` `i = 0 ; i < n ; i++) ` `    ``{ ` `        ``for` `(``int` `j = 0 ; j < n ; j++) ` `        ``{ ` `            ``// v1 for first and second array ` `            ``v1.push_back(a[i]^b[j]); ` ` `  `            ``// v2 for third and forth array. ` `            ``// x is a constant, so no need for ` `            ``// a separate loop ` `            ``v2.push_back(x ^ c[i] ^ d[j]); ` `        ``} ` `    ``} ` ` `  `    ``// Sorting the first set (Containing XOR ` `    ``// of a[] and b[] ` `    ``sort(v1.begin(), v1.end()); ` ` `  `    ``// Finding the lower and upper bound of an ` `    ``// element to find its number ` `    ``for` `(``int` `i = 0 ; i < v2.size() ; i++) ` `    ``{ ` `        ``// Count number of occurrences of v2[i] in sorted ` `        ``// v1[] and add the count to result. ` `        ``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; ` `} ` ` `  `// Driver Program ` `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); ` ` `  `    ``cout << findQuadruples(a, b, c, d, x, n) << endl; ` ` `  `    ``return` `0; ` `} `

Output:

`4`

Time Complexity: O(n2log(n))
Auxiliary Space: O(n2)

This article is contributed by Shubham Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up

Improved By : nitin mittal, vt_m

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.