Easy C++ | set | Count set bits | Sort | O(nlogn)
OR + AND is basically sum of count bits of a and b;\n(a OR b) = count(a) + count(b) - count(common(a,b)\n(a AND b) = count(common(a,b)\n\nTherefore, (a OR b) + 
OR + AND is basically sum of count bits of a and b;\n(a OR b) = count(a) + count(b) - count(common(a,b)\n(a AND b) = count(common(a,b)\n\nTherefore, (a OR b) + (a AND b) = count(a) + count(b)\n\n```\n#define ll long long\n\nclass Solution {\npublic:\n    \n    ll countSetBits(ll n) {\n    if (n == 0)\n        return 0;\n    else\n        return 1 + countSetBits(n & (n - 1));\n    }\n    \n    ll countExcellentPairs(vector<int>& nums, int k) {\n        ll n = nums.size();\n        \n        set<ll> vis;\n        vector<ll> v;\n        \n        for(ll i=0;i<n;i++){\n            // count set bits of unique elements and push it into vector\n            if(vis.find(nums[i]) == vis.end()){\n                vis.insert(nums[i]);\n                ll x = countSetBits(nums[i]);\n                v.push_back(x);\n            }\n        }\n        \n        // sort the vector\n        sort(v.begin(), v.end());\n        \n        // stores final answer\n        ll cnt = 0;\n        \n        ll m = v.size();\n        \n        for(int i=0;i<m;i++){\n            ll x = v[i];\n            \n            // if set bits is >= k\n            if(x >= k){\n                // for current take 1 and for rest 2 {(3,3) - 1}, {(2,3), (3,2)} - 2\n                ll a = m - i - 1;\n                cnt += (2*a+1);\n            } else {\n                ll rem = k-x;\n                ll y = lower_bound(v.begin()+i, v.end(), rem) - v.begin();\n                \n                if(y == i){\n                    ll a = m-y-1;\n                    cnt += (2*a + 1);\n                    \n                } else {\n                    ll a = m-y;\n                    cnt += (2*a);\n                }\n            }\n        }\n        \n        return cnt;\n    }\n};\n```
[]