sliding window+ sort + prefix sum
Calculating the difference needed to move every number in a window to the median value is a bit messy\n\n# Code\n\nclass Solution:\n    def maxFrequencyScore(se
Calculating the difference needed to move every number in a window to the median value is a bit messy\n\n# Code\n```\nclass Solution:\n    def maxFrequencyScore(self, nums: List[int], k: int) -> int:\n        nums.sort()\n        psum = [0]\n        n = len(nums)\n        for a in nums:\n            psum.append(psum[-1]+a)\n        \n        res = 0\n        i = 0\n        for j in range(n):\n            mid = (i+j) // 2\n            m = nums[mid]\n            diff = m*(mid-i+1) - (psum[mid+1] - psum[i]) + psum[j+1] - psum[mid] - m*(j-mid+1)\n            while i<=j and diff > k:\n                i += 1\n                mid = (i+j) // 2\n                m = nums[mid]\n                diff = m*(mid-i+1) - (psum[mid+1] - psum[i]) + psum[j+1] - psum[mid] - m*(j-mid+1)\n            res = max(j-i+1,res)\n        return res\n\n        \n```
['Python3']