<p>The <strong>frequency</strong> of an element is the number of times it occurs in an array.</p>

<p>You are given an integer array <code>nums</code> and an integer <code>k</code>. In one operation, you can choose an index of <code>nums</code> and increment the element at that index by <code>1</code>.</p>

<p>Return <em>the <strong>maximum possible frequency</strong> of an element after performing <strong>at most</strong> </em><code>k</code><em> operations</em>.</p>

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

<pre>
<strong>Input:</strong> nums = [1,2,4], k = 5
<strong>Output:</strong> 3<strong>
Explanation:</strong> Increment the first element three times and the second element two times to make nums = [4,4,4].
4 has a frequency of 3.</pre>

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

<pre>
<strong>Input:</strong> nums = [1,4,8,13], k = 5
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are multiple optimal solutions:
- Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2.
- Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2.
- Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2.
</pre>

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

<pre>
<strong>Input:</strong> nums = [3,9,6], k = 2
<strong>Output:</strong> 1
</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>5</sup></code></li>
	<li><code>1 &lt;= k &lt;= 10<sup>5</sup></code></li>
</ul>

class Solution:
    def maxFrequency(self, numbers: List[int], k: int) -> int:
        numbers.sort()

        ans = 0

        # 1 3 4
        # 1 4 8     (4 + 4 + 4) - (1 + 3 + 4) = 12 - 8 = 4

        # 1 2 4
        # 1 3 7     (4 + 4 + 4) - (1 + 2 + 4) = 12 - 7 = 5

        # 1 1 4     
        # 1 2 6     (4 + 4 + 4) - (1 + 1 + 4) = 12 - 6 = 6
 

        # 1 2 3  4  5  6  7  8     k = 7
        #     l           r
        # 1 3 6 10 15 21 28 36
        #                          
        # 7 (max number) * 5 (length) = 35
        # {1 2 3  4  5  6  7} - {1 2 3} = 28 - 6 = 22 = {4  5  6  7} + 3 = 25
        # 35 - 25 = 10 need to add, is it true?  3 + [4], 4 + [3], 5 + [2], 6 + [1] = 4+3+2+1=10 ok

        cache = []
        for number in numbers:
            if cache == []:
                cache.append(number)
            else:
                cache.append(cache[-1] + number)
        
        for index, number in enumerate(numbers):
            # Find leftmost_index using binary serach such that all
            # [leftmost_index, index] elements will be equal to numbers[index]
            # and sum of added elements <= k
            l = 0
            r = index
            while l < r:
                c = (l + r) // 2
                total_sum_when_added_to_max = number * (index - c + 1)
                current_sum = cache[index] - cache[c] + numbers[c]
                need_to_add = total_sum_when_added_to_max - current_sum
                if need_to_add <= k:
                    r = c # go to the left part of searhing start element, i.e start left such that we have more elements
                else:
                    l = c + 1
            
            total_sum_when_added_to_max = number * (index - l + 1)
            current_sum = cache[index] - cache[l] + numbers[l]
            need_to_add = total_sum_when_added_to_max - current_sum
            if need_to_add <= k:
                ans = max(ans, index - l + 1)

        return ans

29500