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

//https://hyper-meta.blogspot.com/
#pragma GCC target("avx,mmx,sse2,sse3,sse4")
auto _=[]()noexcept{ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);return 0;}();
int fre[100001];
class Solution {
public:
    int maxFrequency(vector<int>& nums, int k) {
        const int sz=nums.size();
        if(sz>1500){
            memset(fre,0,sizeof(fre));
            for(auto e:nums) fre[e]++;
            int i=1;
            while(fre[i]==0) ++i;
            int pre=i,r=fre[pre],wl=i;
            long long inc=0,pcnt=r;
            i++;
            while(1){
                while(i<=100000&&fre[i]==0)++i;
                if(i==100001) break;
                int cur=i;
                inc+=(cur-pre)*pcnt;
                while(inc>k){
                    while(fre[wl]==0) wl++;
                    --fre[wl];
                    inc-=cur-wl;
                    --pcnt;
                }
                pcnt+=fre[i++];
                r=max(r,(int)pcnt);
                pre=cur;
            }
            return r;
        }
        sort(nums.begin(),nums.end());
        int i=1,pre=nums[0];
        while(i<sz&&nums[i]==pre) ++i;
        int r=i,wl=0;
        long long inc=0,pcnt=i;
        while(i<sz){
            int cur=nums[i++];
            inc+=(cur-pre)*pcnt;
            while(inc>k) {inc-=cur-nums[wl++];--pcnt;}
            pcnt++;
            while(i<sz&&nums[i]==cur){++i;++pcnt;}
            r=max(r,i-wl);
            pre=cur;
        }
        return r;
    }
};
105200