✅✅Faster || Easy To Understand || C++ Code
Using Sorting And Binary Search\n\n Time Complexity :- O(NlogN)\n\n Space Complexity :- O(N)\n\n\nclass Solution {\npublic:\n    \n    // function for counting 
* ***Using Sorting And Binary Search***\n\n* ***Time Complexity :- O(NlogN)***\n\n* ***Space Complexity :- O(N)***\n\n```\nclass Solution {\npublic:\n    \n    // function for counting no. of set bits in num\n    \n    int find_set_bits(int num)\n    {\n        int count = 0;\n        \n        while(num)\n        {\n            count++;\n            \n            num = (num & (num - 1));\n        }\n        \n        return count;\n    }\n    \n    long long countExcellentPairs(vector<int>& nums, int k) {\n        \n        int n = nums.size();\n        \n        sort(nums.begin(), nums.end());\n        \n        // make unique array, to avoid duplicates pairs\n        \n        vector<int> arr;\n        \n        int i = 0;\n        \n        while(i < n)\n        {\n            while(i < n - 1 && nums[i] == nums[i + 1])\n            {\n                i++;\n            }\n            \n            arr.push_back(nums[i]);\n            \n            i++;\n        }\n        \n        // set_bit array will store the no. of set bits presents in arr[i]\n       \n        vector<int> set_bit;\n        \n        for(int i = 0; i < arr.size(); i++)\n        {\n            int count = find_set_bits(arr[i]);\n           \n            set_bit.push_back(count);\n        }\n        \n        // sort the set_bit array\n        \n        sort(set_bit.begin(), set_bit.end());\n        \n        // find all the unique pairs\n        \n        long long count = 0;\n        \n        for(int i = 0; i < arr.size(); i++)\n        {\n            // no. of minimum set bits required, so that sum can be greater than k\n            \n            int need = k - set_bit[i];\n            \n            // upply lower bound on set_bits\n            \n            int idx = lower_bound(set_bit.begin(), set_bit.end(), need) - set_bit.begin();\n            \n            // update the count\n            \n            count += set_bit.size() - idx;\n        }\n        \n        return count;\n    }\n};\n```
['Binary Search', 'Bit Manipulation', 'C', 'Sorting', 'Binary Tree', 'C++']