C++ Easy solution O(N).
\n\nNotice that OR(num1, num2) + AND(num1, num2) = sum_of_bits(num1) + sum_of_bits(num2).\n\n\nclass Solution {\npublic:\n    long long countExcellentPairs(vect
\n\nNotice that `OR(num1, num2) + AND(num1, num2) = sum_of_bits(num1) + sum_of_bits(num2)`.\n\n```\nclass Solution {\npublic:\n    long long countExcellentPairs(vector<int>& nums, int k) {\n        vector<long long> cnt(32);\n        set<int> s;\n        for(auto n : nums) {\n            s.insert(n);\n        }\n        for(auto n : s) {\n            ++cnt[__builtin_popcount(n)];\n        }\n        long long ans = 0;\n        \n        for(auto n : s) {\n            int bits = __builtin_popcount(n);\n            int r = max(0, k - bits);\n            for(int i = r; i < 32; i++) {\n                ans += cnt[i];\n            }\n        }\n        return ans;\n    }\n};\n```\n
[]