CPP | O( n log n ) time, O(n) space
Since we have number of \nbits set in ( a | b ) + bits set in ( a & b ) = bits set in a + bits set in b\n\nint bits(int a){\n    int cnt = 0;\n    while(a) a&=a
Since we have number of \nbits set in ( a | b ) + bits set in ( a & b ) = bits set in a + bits set in b\n```\nint bits(int a){\n    int cnt = 0;\n    while(a) a&=a-1,cnt++;\n    return cnt;\n}\n// Number of bits set in a\nclass Solution {\npublic:\n    long long countExcellentPairs(vector<int>& nums, int k) {\n        vector<pair<int,int>> A;\n        int x = -1;\n        sort(nums.begin(),nums.end());\n        for(int a : nums) if(x == a) continue; else x = a,A.push_back({bits(a),a});\n        // removed any repitions, A[i] = {number of set bits in x , x }\n\t\tsort(A.begin(),A.end());\n\t\t\n        long long ans = 0;\n        int i = 0 , j = A.size()-1,n = A.size();\n\t\t// Using 2 pointers\n        while(i<n){\n            while(j>=0 and k <= A[j].first+A[i].first)\n                --j;\n            ans += n - j - 1;//for all index > j, condition is true for index i.\n            i++;\n        }\n        \n        return ans;\n    }\n};\n```
['Array', 'Two Pointers', 'Bit Manipulation', 'C']