[Python] Explanation with pictures, two pointers,
Notice that for two number A and B, the number of bits from A AND B + A OR B equals the number of bits in A (in binary representation) plus the number of bits i
Notice that for two number `A` and `B`, the number of bits from `A AND B` + `A OR B` equals the number of bits in `A` (in binary representation) plus the number of bits in `B`. Thus we just need to collected the sum of bits of each distinct number from `nums`. \n\n![image](https://assets.leetcode.com/users/images/cbef8fbf-2a50-42eb-be00-bfed83e59bca_1658635281.6611814.png)\n\n\nWe could use two pointers to find all such pairs. \n\n![image](https://assets.leetcode.com/users/images/8aebfa2c-6fac-41e4-8e11-4c17aaccb1c2_1658635285.4133282.png)\n\nA detailed example of how we move two pointers:\n\n![image](https://assets.leetcode.com/users/images/ae57f868-4864-43da-b858-305388e0c9be_1658635289.0178986.png)\n\nOR just use binary search to locate the index `j`, since we have already used O(NlogN) on sorting **Bits**, it doesn\'t bring a larger time complexity.\n\n```\nclass Solution:\n    def countExcellentPairs(self, A: List[int], k: int) -> int:\n        # Count number of bits in (n) in binary\n        def numOfBits(n):\n            ans = 0\n            while n:\n                ans += (n & 1)\n                n >>= 1\n            return ans\n        \n        # Sort the number of bits of each distinct number from A\n        B = sorted([numOfBits(a) for a in set(A)])\n        \n        # Right pointer start at n - 1.\n        n, j, ans = len(B), len(B) - 1, 0\n\n        for i in range(n):\n            # If the starting i, j don\'t meet the condition, continue for the next i.\n            if B[i] + B[j] < k: \n                continue\n            \n            # Move until j = 0 or B[i] + B[j] < k\n            while j >= 1 and B[i] + B[j - 1] >= k:\n                j -= 1\n                \n            # All the number to the right of the pointer j (inclusive) make no less\n            # sum of bits with B[i]\n            ans += n - j\n        \n        return ans\n```
[]