Apply Operations to Maximize Frequency Score (In Java)
Code
# Code
```java []
class Solution {
    public int maxFrequencyScore(int[] nums, long k) {
        Arrays.sort(nums);
        int n = nums.length;
        long a[]=new long[n+1];
        for(int i=0;i<n;i++){
            a[i+1]=a[i]+nums[i];
        }
        int s=0,e=1,o=1;
        while(e<n){
            e++;
            int mid = (s+e)/2;
            long target = nums[mid];
            long cost = (target*(mid-s)-(a[mid]-a[s]))+(a[e]-a[mid]-target*(e-mid));
            while(s<e && cost > k){
                s++;
                mid = (s+e)/2;
                target = nums[mid];
                cost = (target*(mid-s)-(a[mid]-a[s]))+(a[e]-a[mid]-target*(e-mid));
            }
            o = Math.max(o,e-s);
        }
        return o;
    }
}
```
['Java']