<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code> and an integer <code>k</code>.</p>

<p>You can perform the following operation on the array <strong>at most</strong> <code>k</code> times:</p>

<ul>
	<li>Choose any index <code>i</code> from the array and <strong>increase</strong> or <strong>decrease</strong> <code>nums[i]</code> by <code>1</code>.</li>
</ul>

<p>The score of the final array is the <strong>frequency</strong> of the most frequent element in the array.</p>

<p>Return <em>the <strong>maximum</strong> score you can achieve</em>.</p>

<p>The frequency of an element is the number of occurences of that element in the array.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,6,4], k = 3
<strong>Output:</strong> 3
<strong>Explanation:</strong> We can do the following operations on the array:
- Choose i = 0, and increase the value of nums[0] by 1. The resulting array is [2,2,6,4].
- Choose i = 3, and decrease the value of nums[3] by 1. The resulting array is [2,2,6,3].
- Choose i = 3, and decrease the value of nums[3] by 1. The resulting array is [2,2,6,2].
The element 2 is the most frequent in the final array so our score is 3.
It can be shown that we cannot achieve a better score.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,4,4,2,4], k = 0
<strong>Output:</strong> 3
<strong>Explanation:</strong> We cannot apply any operations so our score will be the frequency of the most frequent element in the original array, which is 3.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>
	<li><code>0 &lt;= k &lt;= 10<sup>14</sup></code></li>
</ul>

class Solution:
    def maxFrequencyScore(self, nums: List[int], k: int) -> int:
        if k == 0:
            # Maximum sequence of equal values.
            return max(Counter(nums).values())
        nums.sort()
        prefixes: list[int] = [0 for _ in range(len(nums))]
        suffixes: list[int] = [0 for _ in range(len(nums))]
        for x in range(1, len(nums)):
            prefixes[x] = prefixes[x - 1] + nums[x - 1]
        for x in range(len(nums) - 2, -1, -1):
            suffixes[x] = suffixes[x + 1] + nums[x + 1]
        # Leftmost, rightmost indexes of sliding window.
        left: int = 0
        right: int = 0
        max_score: int = 1
        # Expand.
        while right < len(nums) - 1:
            right += 1
            median: int = (left + right) // 2
            target: int = nums[median]  # value we want in the whole subarray (left -> right, inclusive)
            # Operations to get subarray in balance, i.e make all values equal to target:
            # (sum of values we need in {left -> median, median not included}) - (what we already have)) +
            # + (what we already have) - (sum of values we need in {median -> right, median not included})
            balance_cost: int = ((target * (median - left) - (prefixes[median] - prefixes[left]))
                                + (suffixes[median] - suffixes[right] - target * (right - median)))
            # Shrink, until window is correct.
            while balance_cost > k:
                left += 1
                median = (left + right) // 2
                target = nums[median]
                balance_cost = ((target * (median - left) - (prefixes[median] - prefixes[left]))
                                + (suffixes[median] - suffixes[right] - target * (right - median)))
            max_score = max(max_score, right + 1 - left)  # +1 for 0-indexed.
        return max_score
899