Java | Count Set ones | Sort | BinarySearch | NLogN
\nclass Solution {\n  public long countExcellentPairs(int[] nums, int k) {\n    int n = nums.length;\n\n    HashSet<Integer> set = new HashSet<>();\n    for (in
```\nclass Solution {\n  public long countExcellentPairs(int[] nums, int k) {\n    int n = nums.length;\n\n    HashSet<Integer> set = new HashSet<>();\n    for (int i = n - 1; i >= 0; i--) set.add(nums[i]);\n\n    n = set.size();\n    int[] dist = new int[n];\n    int i = 0;\n    for (int next : set) dist[i++] = Integer.bitCount(next);\n\n    Arrays.sort(dist);\n    long ans = 0;\n\n    for (i = 0; i < n; i++) {\n      int ind = bsearch(dist, k - dist[i]);\n\n      if (ind >= 0) ans += (n - ind + 0l);\n    }\n\n    return ans;\n  }\n\n  public int bsearch(int[] d, int t) {\n    int l = 0;\n    int r = d.length - 1;\n    int ans = -1;\n    while (l <= r) {\n      int mid = (l + r) / 2;\n\n      if (d[mid] < t)\n        l = mid + 1;\n      else {\n        ans = mid;\n        r = mid - 1;\n      }\n    }\n    return ans;\n  }\n}\n```
['Binary Search', 'Sorting', 'Java']