Sliding window || Binary Search || O(n.logn) || Two Pointers
Sliding Window for creating that element as max. freq, not currently present in array and binary search for creating element as max freq. elem present in array.
`
Sliding Window for creating that element as max. freq, not currently present in array and binary search for creating element as max freq. elem present in array.
`
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
$$O(n.logn)$$
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
$$O(1)$$
# Code
```java []
class Solution {
     static public int maxFrequency(int[] arr, int k, int numOperations) {
        Arrays.sort(arr);
        int n = arr.length, i = 0, j = 0, max1 = 0;
        while (i < n && j < n) {
            int current = arr[i];
            while (j < n && arr[j] <= (current + 2 * k) && (j - i) < numOperations) {
                j++;
            }
            max1 = Math.max(max1, j - i);
            i++;
        }
        
        int max2 = 0;
        for (i = 0; i < n; i++) {
            int current = arr[i];
            int left = LB(arr, current - k, i);
            int right = UB(arr, current + k, i), freq = 0;
            while(i < n && arr[i] == current){
                i++;
                freq++;
            }
            i--;
            if (right - left + 1 > numOperations) {
                int remain = (right - left + 1) - freq;
                if(remain >= numOperations) remain = numOperations;
                max2 = Math.max(max2, remain + freq);
            } else
                max2 = Math.max(max2, right - left + 1);
           
        }

        return Math.max(max1, max2);
    }

    static int LB(int[] arr, int x, int n) {
        int start = 0, end = n, mid = 0, ans = n;
        while (start <= end) {
            mid = start + (end - start) / 2;
            if (arr[mid] >= x) {
                end = mid - 1;
                ans = mid;
            } else
                start = mid + 1;
        }
        return ans;
    }

    static int UB(int[] arr, int x, int n) {
        int start = n, end = arr.length - 1, mid = 0, ans = n;
        while (start <= end) {
            mid = start + (end - start) / 2;
            if (arr[mid] <= x) {
                start = mid + 1;
                ans = mid;
            } else
                end = mid - 1;
        }
        return ans;
    }
}
```
['Binary Search', 'Sliding Window', 'Sorting', 'Java']