C++ Solution with T.C :- O(N*Log N) and S.C :- O(N) Using BS on Answer Concept.
\nclass Solution {\npublic:\n    #define ll long long \n    int maxFrequencyScore(vector<int>& nums, long long k) {\n        int n=nums.size();\n        int l=1
```\nclass Solution {\npublic:\n    #define ll long long \n    int maxFrequencyScore(vector<int>& nums, long long k) {\n        int n=nums.size();\n        int l=1,r=n;\n        vector<ll> pf(n);\n        sort(nums.begin(),nums.end());\n        pf[0]=nums[0];\n        for(int i=1;i<n;++i){\n            pf[i]=pf[i-1]+1LL*nums[i];\n        }\n        // This question is of the form TTTTTTTFFFFFFF first the monotonic function is increasing and then it is decreasing.\n        while(l<=r){\n            ll md=l+(r-l)/2;\n            ll i=0,j=md-1,m=(i+j)/2;\n            bool f=false;\n            while(j<n){\n                m=(i+j)/2;\n                ll op=0;\n                if(i){\n                    op = nums[m]*1LL*(m-i+1) - (pf[m]-pf[i-1]) + (pf[j]-pf[m]) - (j-m)*1LL*nums[m];\n                }else{\n                    op = nums[m]*1LL*(m-i+1) - (2LL*pf[m]) + pf[j] - (j-m)*1LL*nums[m];\n                }\n                if(op <= k) {f=true;break;}\n                i++; j++;\n            }\n            if(f){\n                l=md+1;\n            }else{\n                r=md-1;\n            }\n        }\n        return r;\n    }\n};\n```
['Array', 'C', 'Sliding Window', 'Sorting', 'Binary Tree', 'Prefix Sum']