<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 {
public:
    int maxFrequency(vector<int>& nums, int k) {
        sort(nums.begin(), nums.end());  // Step 1: Sort the array
        unordered_map<int, int> freqMap; // Step 2: Hash map to store frequency of elements in the window
        long long sum = 0;               // Sum of elements in the current window
        int maxFreq = 0, left = 0;       // Maximum frequency and left pointer of the sliding window
        
        for (int right = 0; right < nums.size(); ++right) {
            sum += nums[right];  // Add current element to the window's sum
            freqMap[nums[right]]++; // Track frequency of current element
            
            // Step 3: While the cost of making all elements in the window equal to nums[right] exceeds k
            while ((long long)(right - left + 1) * nums[right] - sum > k) {
                sum -= nums[left];    // Remove the leftmost element from the window's sum
                freqMap[nums[left]]--; // Decrease frequency count of the leftmost element
                if (freqMap[nums[left]] == 0) {
                    freqMap.erase(nums[left]);  // Clean up map when frequency is 0
                }
                left++;  // Shrink the window from the left
            }
            
            // Step 4: Update the maximum frequency
            maxFreq = max(maxFreq, right - left + 1);
        }
        
        return maxFreq;  // Return the maximum frequency found
    }
};

115300