Easy Cpp Solution Using Set Bits Logic
Intuition\n Describe your first thoughts on how to solve this problem. \nFirstly remove the duplicate elements from the vector and then find the setbits of each
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nFirstly remove the duplicate elements from the vector and then find the setbits of each element and check whether it is greater than or equal to k.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nStep 1: Store the unique elements only in a vector `num` using set `s`.\nStep 2: Calculate setBits of each element and store them in a vector `v`\nStep 3: Sort the vector `v`.\nStep 4: Traverse the vector `v` and calculate the number of pairs.\nStep 5: If element is greater than `k` then add `2 * i + 1` to the result.\nStep 6: Else find the suitable index and add `(i - index) * 2 + 1` to the result.\nStep 7: Return the result.\n\n# Complexity\n- Time complexity: $$O(nlogn)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int search(vector<int>&v, int end, int target)\n    {\n        int beg = 0;\n        int ind = -1;\n        while(beg <= end)\n        {\n            int mid = beg + (end - beg) / 2;\n            if(v[mid] >= target)\n            {\n                ind = mid;\n                end = mid - 1;\n            }\n            else\n                beg = mid + 1;\n        }\n        return ind;\n    }\n    long long countExcellentPairs(vector<int>& nums, int k) {\n        set<int>s;\n        vector<int>v;\n        vector<int>num;\n        long long res = 0;\n\n        for(int i=0;i<nums.size();i++)\n            s.insert(nums[i]);\n\n        for(auto i:s)\n            num.push_back(i);\n        \n        for(int i=0;i<num.size();i++)\n        {\n            int n = num[i];\n            int cnt = 0;\n            while(n)\n            {\n                n = n & (n-1);\n                cnt++;\n            }\n            v.push_back(cnt);\n        }\n        sort(v.begin(), v.end());\n\n        for(int i=0;i<v.size();i++)\n        {\n            if(v[i] >= k)\n                res += 2*i + 1;\n            else\n            {\n                int ind = search(v, i, k-v[i]);\n                if(ind != -1)\n                    res += (i - ind)*2 + 1;\n            }\n        }\n        return res;\n\n    }\n};\n```\n\n# DAY 20 DONE !!
['C++']