Easy Solution using 2 Binary Searches
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```cpp []\nclass Solution {\npublic:\n    int maxFrequencyScore(vector<int>& nums, long long k) {\n        int n = nums.size();\n        sort(nums.begin(), nums.end());\n\n        vector<long long> pref(n + 1, 0);\n        for (int i = 0; i < n; ++i) {\n            pref[i + 1] = pref[i] + nums[i];\n        }\n\n        int ans = 0;\n\n        for (int i = 0; i < n; i++) {\n            int lo = i, hi = n, j;\n            while (hi - lo > 1) {\n                j = (hi + lo) / 2;\n                int len = j - i + 1;\n\n                int val = (len % 2) ? nums[(i + j) / 2] : (nums[(i + j) / 2] + nums[(i + j) / 2 + 1]) / 2;\n\n                auto ind = lower_bound(nums.begin(), nums.end(), val) - nums.begin();\n\n                long long leftCost = val * (ind - i) - (pref[ind] - pref[i]);\n                long long rightCost = (pref[j + 1] - pref[ind]) - val * (j - ind + 1);\n                long long totalCost = leftCost + rightCost;\n\n                if (totalCost <= k) {\n                    lo = j;  \n                } else {\n                    hi = j;\n                }\n            }\n\n            ans = max(ans, lo - i + 1);\n        }\n\n        return ans;\n    }\n};\n\n```
['C++']