Frequency of the Most Frequent Element LeetCode solution in Java

1
class Solution {
2
public int maxFrequency(int[] nums, int k) {
3
// Step-1: Sorting->
4
Arrays.sort(nums);
5
// Step-2: Two-Pointers->
6
int L = 0, R = 0;
7
long totalSum = 0;
8
int res = 1;
9
// Iterating over the array:
10
while (R < nums.length) {
11
totalSum += 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]"
15
while (!((totalSum + k) >= ((R - L + 1) * nums[R]))) {
16
totalSum -= nums[L];
17
L++;
18
}
19
res = Math.max(res, (R - L + 1));
20
R++;
21
}
22
return res;
23
}
24
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0