The frequency of an element is the number of times it occurs in an array.

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

Return the maximum possible frequency of an element after performing at most k operations.

&nbsp;
Example 1:

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

Example 2:

Input: nums = [1,4,8,13], k = 5
Output: 2
Explanation: 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.


Example 3:

Input: nums = [3,9,6], k = 2
Output: 1


&nbsp;
Constraints:


	1 &lt;= nums.length &lt;= 105
	1 &lt;= nums[i] &lt;= 105
	1 &lt;= k &lt;= 105


class Solution:
    def maxFrequency(self, nums: List[int], k: int) -> int:
        nums.sort()
        sums, i, ans = 0, 0, 0
        for j in range(len(nums)):
            sums += nums[j]
            while nums[j]*(j-i+1) > sums+k:
                sums -= nums[i]
                i = i+1
            ans = max(ans, j-i+1)
        return ans
class Solution {
    public int maxFrequency(int[] nums, int k) {
        //Step-1: Sorting->
        Arrays.sort(nums);
        //Step-2: Two-Pointers->
        int L=0,R=0;
        long totalSum=0;
        int res=1;
        //Iterating over the array:
        while(R<nums.length)
        {
            totalSum+=nums[R];
            //The value of "totalSum+k" should be ">=" "windowSize*nums[R]"
            //then only the window is possible else decrease the "totalSum"
            //till the value "totalSum+k" is ">=" "windowSize*nums[R]"
            while(! ((totalSum+k) >= ((R-L+1)*nums[R])) )
            {
                totalSum-=nums[L];
                L++;
            }
            res=Math.max(res,(R-L+1));
            R++;
        }
        return res;
    }
}
#define ll long long

class Solution {
public:
    int maxFrequency(vector<int>& nums, int k) {
        // sorting so that can easily find the optimal window
        sort(nums.begin(), nums.end());

        // left - left pointer of window
        // right - right pointer of window
        int left = 0, right = 0, ans = 1;
        ll total = 0, n = nums.size();
        while(right < n){
            // total - total sum of elements in the window
            total += nums[right];

            // Checking if the we can achieve elements in this window
            // If it exceeds k then shrinking the window by moving left pointer
            // For optimal we will make all elements in the array equal to
            // the maximum value element
            while((1ll)*(right - left + 1)*nums[right] - total > k){
                total -= nums[left];
                left++;
            }

            ans = max(ans, right - left + 1);
            right++;
        }
        return ans;
    }
};
var maxFrequency = function(nums, k) {
// We sorted because as we are allowed only to increment the value & we try to increase the smaller el to some larger el
    nums.sort((a,b)=>a-b);
    
    let left=0;
    let max=Math.max(); // without any args, Math.max() is -Infinity
    let curr=0;
	// I have used 'for loop' so rightPtr is 'i' here
    for(let i=0;i<nums.length;i++){
        curr+=nums[i];
		// decrement the winSize once the value required to convert could not be achieved even after utilizing K
		// (i-left+1) * nums[i] because we are converting every el upto that winSize by current el
        while((i-left+1) * nums[i] > curr+k){
            curr-=nums[left++]
        }        
        max = Math.max(max,i-left+1);
    }
    return max;
};
Frequency of the Most Frequent Element