<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(std::vector<int>& nums, int k) {
        countingSort(nums); // Sort the array using counting sort
        int start = 0; // Start index of the current subarray
        long long subarraySum = 0; // Sum of elements in the current subarray
        int maxFrequency = 1; // Initialize the maximum frequency to 1
        for (int i = 0; i < nums.size(); i++) {
            int subarrayLength = i - start + 1; // Length of the current subarray
            long long subarrayProduct = (long long)nums[i] * subarrayLength; // Use long long to avoid overflow
            subarraySum += nums[i]; // Add the current element to the subarray sum

            // Adjust the subarray to satisfy the condition (product - sum <= k)
            while (subarrayProduct - subarraySum > k) {
                subarraySum -= nums[start]; // Remove the leftmost element from the subarray
                start++; // Move the start index to the right
                subarrayLength--; // Decrease the length of the subarray
                subarrayProduct = (long long)nums[i] * subarrayLength; // Recalculate the product
            }

            // Update the maximum frequency based on the current subarray length
            maxFrequency = std::max(maxFrequency, subarrayLength);
        }
        return maxFrequency;
    }

private:
    void countingSort(std::vector<int>& nums) {
        int maxElement = INT_MIN;
        for (int num : nums) {
            maxElement = max(maxElement, num);
        }
        std::vector<int> countMap(maxElement + 1, 0);
        for (int num : nums) {
            countMap[num]++;
        }
        int index = 0;
        for (int i = 0; i <= maxElement; i++) {
            while (countMap[i]-- > 0) {
                nums[index++] = i;
            }
        }
    }
};
113500