C++ - Binary search
\n\n# Code\n\nclass Solution {\n    bool possible(const int len,const int index,const vector<long long>& prefix, const vector<int>& nums,const long k){\n       
\n\n# Code\n```\nclass Solution {\n    bool possible(const int len,const int index,const vector<long long>& prefix, const vector<int>& nums,const long k){\n        int med = index + len/2;\n        long long medSum = 1LL*nums[med]*(med-index)-prefix[med]+prefix[index] + 1LL*(prefix[index+len]-prefix[med+1])-1LL*nums[med]*(index+len-1-med);\n        return medSum<=k;\n    }\npublic:\n    int maxFrequencyScore(vector<int>& nums, long long k) {\n        sort(nums.begin(),nums.end());\n        int l,r,sz=nums.size(),m;\n        vector<long long> prefix (sz+1,0);\n        for(int i=1;i<=sz;i++) prefix[i] = nums[i-1]+prefix[i-1];\n        int ans=0;\n        for (int i=0;i<sz;i++){\n            l=1,r=sz-i;\n            while(l<=r){\n                m=l+(r-l)/2;\n                if (possible(m,i,prefix,nums,k)){\n                    l=m+1;\n                }\n                else{\n                    r=m-1;\n                }\n            }\n            ans = max(ans,l-1);\n        }\n        return ans;\n    }\n};\n```
['C++']