Trends

Count the XOR-friendly pairs

Trending 1 year ago
beritaja.com

Given an array A[], nan task is to count nan number of XOR-friendly pairs.

An XOR-friendly pair is defined arsenic a brace of elements for which nan greatest of nan premier factors of nan XOR of nan 2 elements of nan brace is greater than nan quality of elements of nan brace itself.

Examples:

Input: A[] = {6, 8, 4} 
Output: 1
Explanation: For each pairs:
Pair: 6 8 (diff = 2), XOR: 14, pfs:  2  7 (7 > 2) 
Pair: 6 4 (diff = 2), XOR: 2, pfs:  2        
Pair: 8 4 (diff = 4), XOR: 12, pfs:  2  2  3   

Only for brace (6, 8) nan top premier facet 7 is greater than nan quality 2

Input: A[] = {9, 6, 4, 8}
Output: 3
Explanation: For each pairs:
Pair: 9 6 (diff = 3), XOR: 15, pfs: 3 5
Pair: 9 4 (diff = 5), XOR: 13, pfs: 13
Pair: 9 8 (diff = 1), XOR: 1, pfs: No premier factors for 1
Pair: 6 4 (diff = 2), XOR: 2, pfs: 2
Pair: 6 8 (diff = 2), XOR: 14, pfs: 2 7
Pair: 4 8 (diff = 4)   XOR: 12, pfs: 2 3 

For pairs (9, 6), (9, 4), (6, 8) nan top premier facet is greater than nan quality of nan pair

Approach: To lick nan problem travel nan beneath idea:

This solution is simple, utilizing 2 for-loops we iterate done each nan pairs, and for each pair, we find their quality and XOR. Then utilizing nan quadrate guidelines method we find each premier factors of nan XOR and shop them successful an array past find nan top of each nan premier numbers. Now cheque if nan top premier facet of nan XOR of nan brace of elements is greater than nan quality of nan brace of elements if it is past we person recovered 1 specified pair. Similarly, we find each specified pairs.

Follow nan beneath steps to lick nan problem:

  • Define a information structure: vector of pairs that will shop nan required pairs.
  • Two nested loops are utilized to iterate done each nan pairs of nan array.
  • For each brace compute nan bitwise XOR past find each premier factors of nan XOR and shop them successful different array (for this solution nan quadrate guidelines method of uncovering premier factors has been used) 
  • Find nan largest of nan premier factors from nan array created successful nan supra step.
  • check if nan largest premier facet is greater than nan quality of nan pair, if it is past push nan brace into nan information structure.
  • When some nan loops extremity return nan size of nan information building arsenic it will incorporate only those pairs whose quality is smaller than nan top premier facet of their XOR.

Below is nan implementation of nan supra approach:-

C++

#include <bits/stdc++.h>

using namespace std;

vector<int> numOfPrimeFacts(int n)

{

    vector<int> a;

    if (n == 1) {

        a.push_back(0);

        return a;

    }

    for (int one = 2; (i * i) <= n; i++) {

        while (n % one == 0) {

            a.push_back(i);

            n = n / i;

        }

    }

    if (n > 1)

        a.push_back(n);

    return a;

}

int greatestPrimeFact(vector<int> v)

{

    int ans = 0;

    for (int one = 0; one < v.size(); i++)

        if (v[i] > ans)

            ans = v[i];

    return ans;

}

vector<pair<int, int> > returnPair(int arr[], int n)

{

    vector<pair<int, int> > vec;

    for (int one = 0; one < n; i++) {

        for (int j = one + 1; j < n; j++) {

            pair<int, int> p;

            p.first = arr[i];

            p.second = arr[j];

            int val = arr[i] ^ arr[j];

            int diffOfPair = abs(arr[i] - arr[j]);

            vector<int> a = numOfPrimeFacts(val);

            int largestPrimeFact = greatestPrimeFact(a);

            if (largestPrimeFact > diffOfPair)

                vec.push_back(p);

        }

    }

    return vec;

}

int main()

{

    int arr[] = { 9, 6, 4, 8 };

    int n = 4;

    vector<pair<int, int> > vec = returnPair(arr, n);

    cout << "The number of specified pairs is " << vec.size()

         << '\n';

    cout << "The pair(s) is/are :\n";

    cout << "[";

    for (int one = 0; one < vec.size(); i++) {

        cout << " ( " << vec[i].first << ", "

             << vec[i].second << " ) ";

    }

    cout << "]";

    return 0;

}

Output

The number of specified pairs is 3 The pair(s) is/are : [ ( 9, 6 ) ( 9, 4 ) ( 6, 8 ) ]

Time Complexity: O(n^2 x  sqrt(n) * k  + n), wherever n is nan XOR of nan pair, and k is nan number of premier factors.
Auxiliary Space: O(n + k)


Editor: Naga



Read other contents from Beritaja.com at
More Source



close