<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>

# 2968. Apply Operations to Maximize Frequency Score
class Solution:
    def maxFrequencyScore(self, nums: List[int], k: int) -> int:
        # It seems that the median minimize the absolution deviation (l1 dorm deviation)
        # Use a sliding window of increasing size (max_freq) to check the absolution deviation
        # of the subsequence sorted nums. 
        nums.sort()
        n = len(nums)
        max_freq = 1
        
        # start at i, length = max_freq
        for i in range(n): 
            if i == 0: 
                # m_index: index for median, 3.5 if median = (nums[3]+nums[4])/2
                max_freq = 1
                m_index = 0
                mm = 0
                m = nums[m_index]
                num_op = 0
            else: 
                m_index +=0.5
                num_op -=m-nums[i-1]

            while max_freq<n-i:
                # m = median[i, i+max_freq-1]
                max_freq+=1
                # cumulatively update the index for median
                m_index +=0.5
                prev_mm = mm 
                prev_m = m 
                mm = math.floor(m_index)
                if m_index == mm:
                    m = nums[mm]
                else: 
                    m = math.floor((nums[mm]+nums[mm+1])*0.5+0.5)
                # Need to cumuratively update the absolution deviation
                prev_num_op = num_op
                num_op+=(prev_mm-i+1)*(m-prev_m) # nums[j]<=prev_m
                num_op-= (i+max_freq-mm-2)*(m-prev_m) # nums[j]>=m
                num_op+=nums[i+max_freq-1]-m 
                for j in range(prev_mm+1,mm+1):
                    # prev_m <= nums[j] <= m
                    num_op += -(nums[j]-prev_m) + (m-nums[j])
                if num_op>k:
                    max_freq-=1
                    break
        return max_freq
914