Best way to understand this problem
KEY IDEA :   n(A \u222A B) = n(A) + n(B) \u2013 n(A \u2229 B)\nIt is required in the problem that we have to find count of pairs where \nnumber of set bits of (
**KEY IDEA :   n(A \u222A B) = n(A) + n(B) \u2013 n(A \u2229 B)**\nIt is required in the problem that we have to find count of pairs where \nnumber of set bits of (A | B)  + number of set bits of (A & B) >= k\nas  **union** in mathematics implies  **OR** in boolean algebra and **intersection** implies **AND** operation in boolean algebra.\ntherefore: \nn(A | B)= n(A) + n( B) - n( A & B)\n**n(A) +n (B) = n(A| B) + n(A& B)**\n which imples \n number of set bits of A + number of set bits of B = number of set bits (A) **OR** number of set bits of (B) + number of set bits( A) **AND** number of set bits(B) \n Algorithm :\n*  make a unique set of given integers and a initialize variable ans= 0.\n*  Build a 32 size integer array to store the frequency of the numbers having same number of bits. ( An integer can hold up to 32 bits therefore 32 size array is used) .\n*  Now we use two nested loops with iteration from 1 to 32 , to make pairs of A and B  and check if ( setbits(A ) + setBits (B) >=k) then ans = ans+ frequency[ bits(A) ]* freq[ bits(B)]\n*  return the answer\n\n   C++ code\n ```\nclass Solution {\npublic:\n    long long countExcellentPairs(vector<int>& nums, int k) {\n      long long ans=0;\n      int freq[31]={};\n        set<int>s(nums.begin(),nums.end());\n        for(int i:s)\n            freq[cnt(i)]++;\n        for(int i=1;i<=30;i++){\n            for(int j=1;j<=30;j++){\n                if(i+j>=k)\n                    ans+=freq[i]*freq[j];\n            }\n        }\n        return ans;\n    }\n    int cnt(int n){\n        int c=0;\n        while(n){\n            n=n&n-1;\n            c++;\n        }\n        return c;\n    }\n};\n```\n\n\n**PLEASE UPVOTE IF YOU FIND IT HELPFUL**\n\n\n
['Math']