C++ || O(nlogn) Solution
I think to solve this question the most important thing was to observe that sum of \n(A OR B) + (A AND B) will be equal to the sum of the set bit in (A)+ set bi
**I think to solve this question the most important thing was to observe that sum of \n(A OR B) + (A AND B) will be equal to the sum of the set bit in (A)+ set bit in (B) and rest its simple.\nOne more important thing is to remove the duplicates from the initial array at the starting only because if suppose we have array like ->[1,2,5,1]. In this case (1,2) and (2,1) both can be formed using indx 0 and 1 but the same 2 pairs can also be formed using index (1 and 3 ).\nSo you need to take care of that.\nOnce you find out their set bits counts then you just sort them and by using Binary search you can just count how many elements are their that can sum up with current element to form number greater than K.\nAlso i have used vector and not set when i am storing the count of each distinct element that was present in initial array Nums ,this is because 2 distinct element can have same number of set bits and if i use set we would lost out one element so inorder to avoid this thing i have used vector.\nCODE FOR THE FOLLOWING APPROACH**\n\n\nclass Solution {\npublic:\n\n    long long countExcellentPairs(vector<int>& nums, int k) {\n        set<int> s;\n        vector<int> v;\n\t\tfor(auto x: nums){        //Removing Duplicates using set\n            s.insert(x);\n        }\n\t\t\n        auto it=s.begin();\n        while(it!=s.end()){\n            int n=*it,cnt=0;\n\t\t\twhile(n>0){                //Finding count of set bit of the remaining distinct elements and storing them in vector\n                if(n&1){\n                    cnt++;\n                }\n                n=n>>1;\n            }\n            it++;\n            v.push_back(cnt);\n        }\n\t\t\n        long long ans=0;\n        int sze=v.size();\n\t\t\n        sort(v.begin(),v.end());       //Sorting the count array(Set bits count)\n        for(int i=0;i<sze;i++){\n\t\t //Here Suppose k=3 and our current count is 2 then we need elements     \n\t\t // having count atleast >=1 inorder to Fullfill the criteria given\n\t\t \n            int rem=k-v[i];          \n            if(rem<=0){            \n\t\t\t //But Suppose we have a elements that has set bit greater than K \n\t\t\t in that case any element can make pair with him so we add whole array.\n\t\t\t \n                ans+=sze;      \n            }else{\n                int idx=v.end()-lower_bound(v.begin(),v.end(),rem); //if that is not the case then we need \n                ans+=idx;   // to find the least idx that can fullfill our criteria.\n            }\n        }\n        return ans;\n    }\n};\n\n
['Binary Tree']