Frequency of the Most Frequent Element LeetCode solution in Java
1class Solution {2public int maxFrequency(int[] nums, int k) {3// Step-1: Sorting->4Arrays.sort(nums);5// Step-2: Two-Pointers->6int L = 0, R = 0;7long totalSum = 0;8int res = 1;9// Iterating over the array:10while (R < nums.length) {11totalSum += nums[R];12// The value of "totalSum+k" should be ">=" "windowSize*nums[R]"13// then only the window is possible else decrease the "totalSum"14// till the value "totalSum+k" is ">=" "windowSize*nums[R]"15while (!((totalSum + k) >= ((R - L + 1) * nums[R]))) {16totalSum -= nums[L];17L++;18}19res = Math.max(res, (R - L + 1));20R++;21}22return res;23}24}