Count all Quadruples from four arrays such that their XOR equals to ‘x’ in C++

C++Server Side ProgrammingProgramming

In this tutorial, we will be discussing a program to find the number of quadruples from four arrays such that their XOR equals to x.

For this we will be provided with four arrays and a value x. Our task is to count all the quadruples whose XOR is equal to the given value x.

Example

 Live Demo

#include<bits/stdc++.h>
using namespace std;
//counting quadruples with XOR equal to x
int count_quad(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 << count_quad(a, b, c, d, x, n) << endl;
   return 0;
}

Output

4
raja
Updated on 17-Feb-2020 10:39:07

Advertisements