Formula
The important point to realize is that the sum of OR and AND is just the sum of bits of two numbers.\n    \nWe dedup the input array, and count numbers containi
The important point to realize is that the sum of OR and AND is just the sum of bits of two numbers.\n    \nWe dedup the input array, and count numbers containing `n` bits (where n is [1..29]);\n    \nThen, we pick any combination of bits `i` and `j`, such that `i + j >= k`.\n\nIf `i != j`, the number of pairs is `cnt[i] * cnt[j] * 2`; numbers for `i` and `j` are different, so they form two pairs.\n \nIf `i == j`, the number of pairs is `cnt[i] * cnt[i]`. This is different from above, so that pairs with the same number are counted once. \n\n> If `i == j` the number of pairs is `n * (n + 1) / 2`. Then, we multiply it by 2, and we get `n * (n + 1)`. \n> Now, we need to remove `n` elements as they only form one pair. Thus, we get `n * n`.\n\n**Java**\n```java\npublic long countExcellentPairs(int[] nums, int k) {\n    long res = 0, cnt[] = new long[30]; \n    for (int n : Arrays.stream(nums).distinct().toArray()) {\n        ++cnt[Integer.bitCount(n)];\n    }\n    for (int i = 1; i < 30; ++i)\n        for (int j = Math.max(i, k - i); j < 30; ++j)\n            res += cnt[i] * cnt[j] * (i == j ? 1 : 2);\n    return res;\n}\n```\n**C++**\n```cpp\nlong long countExcellentPairs(vector<int>& nums, int k) {\n    long long cnt[30] = {}, res = 0;\n    for (int n : unordered_set<int>(begin(nums), end(nums)))\n        ++cnt[__builtin_popcount(n)];\n    for (int i = 1; i < 30; ++i)\n        for (int j = max(i, k - i); j < 30; ++j)\n            res += cnt[i] * cnt[j] * (i == j ? 1 : 2);\n    return res;\n}\n```\n**Python 3**\nBorrowed from @lee215.\n```python\n  def countExcellentPairs(self, nums, k):\n       c = Counter(map(lambda x: x.bit_count(), set(nums)))\n       return sum(c[k1] * c[k2] for k1 in c for k2 in c if k1 + k2 >= k)\n```
['C', 'Java']