Helpful | Easy | C++ | Fast |
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    long long countExcellentPairs(vector<int>& nums, int k) {\n        long long ans=0;\n        set<int>st;\n        vector<int>v;\n        for(auto i:nums){\n            st.insert(i);\n        }\n        for(auto i:st){\n            int c=0;\n            while(i){\n                c+=(i&1);\n                i>>=1;\n            }\n            v.push_back(c);\n        }\n        sort(v.begin(),v.end());\n\n        for(int i=0;i<v.size();i++){\n            // cout<<v[i]<<" "; \n            auto in=lower_bound(v.begin(),v.end(),max(k-v[i],0));\n            int ind=in-v.begin();\n            // cout<<ind<<" "<<v.size()-max(i+1,ind)<<"\\n";\n            ans+=((v.size()-max(i+1,ind))*2);\n            if(v[i]*2>=k){cout<<"-";ans++;}\n        }\n        return ans;\n    }\n};\n```
['C++']