Java – Binary Search – NlogN Solution
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: \n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\nclass Solution {\n    public int maxFrequency(int[] A, int k, int op) {\n\n        Arrays.sort(A);\n        int n = A.length;\n        HashMap<Integer, Integer> hm = new HashMap<>();\n\n        for (int i = 0; i < n; i++)\n            hm.put(A[i], hm.getOrDefault(A[i], 0) + 1);\n\n        int ans = 0;\n\n        // We dont need to consider all values in [min, max] range.\n        // For each element A[i], only check A[i] - k, A[i], A[i] + k.\n        // Values in (A[i] - k, A[i]) will have same low and high value as A[i] - k.\n        // Same for values in (A[i], A[i] + k).\n\n        for (int i = 0; i < n; i++) {\n\n            for (int j = -1; j <= 1; j++) {\n                int m = A[i] + j * k;\n                int lower_range = m - k;\n                int upper_range = m + k;\n                int low = 0, high = 0;\n\n                int left = 0, right = n - 1, found = 0;\n                while (left <= right) {\n                    int mid = left + (right - left) / 2;\n\n                    if (A[mid] >= lower_range) {\n                        if (A[mid] == lower_range) {\n                            low = mid;\n                            found = 1;\n                        }\n                        right = mid - 1;\n                    } else\n                        left = mid + 1;\n                }\n                if (found == 0)\n                    low = left;\n\n                left = 0;\n                right = n - 1;\n                found = 0;\n                while (left <= right) {\n                    int mid = left + (right - left) / 2;\n\n                    if (A[mid] <= upper_range) {\n                        if (A[mid] == upper_range) {\n                            high = mid;\n                            found = 1;\n                        }\n                        left = mid + 1;\n                    } else\n                        right = mid - 1;\n                }\n                if (found == 0)\n                    high = right;\n\n                int eligible = high - low + 1 - hm.getOrDefault(m, 0);\n\n                int f = 0;\n                if (eligible >= op)\n                    f = op + hm.getOrDefault(m, 0);\n                else\n                    f = eligible + hm.getOrDefault(m, 0);\n\n                ans = Math.max(f, ans);\n            }\n        }\n\n        return ans;\n\n    }\n}\n```
['Binary Search', 'Java']