2354. Number of Excellent Pairs
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```python3 []
class Solution:
    def countExcellentPairs(self, nums: List[int], k: int) -> int:
        unique_counts = sorted(map(self.countSetBits, set(nums)))
        n = len(unique_counts)
        pairs = 0
        for i in range(n):
            target = k - unique_counts[i]
            left = self.findFirstIndex(unique_counts, target)
            pairs += n - left
        return pairs

    def countSetBits(self, num: int) -> int:
        count = 0
        while num:
            num -= num & -num
            count += 1
        return count

    def findFirstIndex(self, arr: List[int], target: int) -> int:
        low, high = 0, len(arr)
        while low < high:
            mid = (low + high) // 2
            if arr[mid] < target:
                low = mid + 1
            else:
                high = mid
        return low

```
['Python3']