Using binary search
\njava []\n\n\nclass Solution {\n    public int maxFrequency(int[] nums, int k, int mi) {\n        int n = nums.length;\n        Map<Integer, Integer> frequency
\n```java []\n\n\nclass Solution {\n    public int maxFrequency(int[] nums, int k, int mi) {\n        int n = nums.length;\n        Map<Integer, Integer> frequencyMap = new HashMap<>();\n        for (int num : nums) {\n            frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1);\n        }\n        Arrays.sort(nums);\n        Set<Integer> events = new HashSet<>();\n        for (int x : nums) {\n            events.add(x);\n            events.add(x - k);\n            events.add(x + k);\n        } \n        int best = 0;\n        for (int i : events) {\n            best = Math.max(best, go(i,nums,frequencyMap,mi,k));\n        }\n        return best;\n    } \n    private int bisectLeft(int[] nums, int target) {\n        int left = 0, right = nums.length-1;\n        int ans=0;\n        while (left <=right) {\n            int mid = left + (right - left) / 2;\n            if (nums[mid] < target) {\n\n                left = mid + 1;\n            }\n            \n            else{\n              ans=mid;\n             right = mid-1;\n            }\n        }\n        return ans;\n    }\n\n    private int bisectRight(int[] nums, int target) {\n    int left = 0, right = nums.length-1;\n    int ans = nums.length;\n    while (left <= right) {\n        int mid = left + (right - left) / 2;\n        if (nums[mid] <= target) {\n            left = mid + 1;\n        } else {\n            ans = mid;\n            right = mid - 1;\n        }\n    }\n    return ans;\n}\n\n       int go(int x,int[]nums,Map<Integer, Integer> frequencyMap,int mi,int k) {\n            \n            int leftIndex = bisectLeft(nums, x - k);\n            int rightIndex = bisectRight(nums, x + k);\n\n            int same = frequencyMap.getOrDefault(x, 0);\n            int possibleChanges = Math.min((rightIndex - leftIndex) - same, mi);\n            return same + possibleChanges;\n        }\n}\n\n```
['Java']