Sliding Window + Prefix Sum
Intuition\n Describe your first thoughts on how to solve this problem. \nSuppose we wanted to change all elements to a value x in an array while minimizing the 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSuppose we wanted to change all elements to a value x in an array while minimizing the number of operations (increment/decrement). The median of such array is the optimal value we should use to minimize the number of operations. We can compute the number of operations using prefix sums and sorting. \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nSort nums. Then we iterate through all valid subarrays using sliding window, and keep track of the maximum length subarray. In order to determine if a subarray is valid, we use a helper function f, and use the prefix sum array to determine the min number of operations to make all elements in the subarray equal. We then simply compare the nubmer of operations to k. The longest such subarray will correspond with the frequency of the most common element. \n# Complexity\n- Time complexity: O(n log n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\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    bool f(vector<long long>& ps, vector<int>& nums, long long k, long long l, long long r){\n        long long m = (l+r) / 2;\n        long long leftSum = ps[m] - (l ? ps[l-1] : 0);\n        long long rightSum = ps[r] - ps[m];\n        return k < nums[m] * (m-l+1) - leftSum + rightSum - nums[m] * (r-m);\n    }\n\n    int maxFrequencyScore(vector<int>& nums, long long k) {\n        sort(nums.begin(), nums.end());\n        int n = nums.size();\n        vector<long long> ps;\n        long long sum = 0;\n        for(int& i:nums){\n            sum += i;\n            ps.push_back(sum);\n        }\n        int res = 1;\n        int l = 0;\n        for(int r=0;r<n;++r){\n            while(l<r && f(ps, nums, k, l, r)){\n                ++l;\n            }\n            res = max(res, r-l+1);\n        }\n        return res;\n    }\n};\n```
['C++']