Home

Shubham and Xor

You are given an array of n integer numbers a1, a2 .. , an.

Calculate the number of pair of indices (i,j) such that 1i < jn and ai xor aj=

Example:

Input:  n = 5, a = [1,3,1,4,3]
Output: 2

Approach

C++

#include <bits/stdc++.h>
using namespace std;

long long numberOfPairs(long long nlong long a[])
{
    sort(aa + n);
    long long i = 0j = 0;
    long long cnt = 0res = 0;
    while (i < n - 1)
    {
        j = i;
        cnt = 1;
        while (i < n - 1 && (a[i] == a[i + 1]))
        {
            i++;
            cnt++;
        }
        res += cnt * (cnt - 1) / 2;
        i++;
    }
    return res;
}
int main()
{
    long long n = 5;

    long long a[n] = {13143};

    cout << numberOfPairs(na<< "\n";

    return 0;
}


No comments:

Post a Comment