BEATS 100% || Java Solution || Optimum Solution
Intuition\n- To find the maximum frequency of an element in an array after performing a certain number of increment operations, we use a combination of frequenc
# Intuition\n- To find the maximum frequency of an element in an array after performing a certain number of increment operations, we use a combination of frequency counting, range updates, and sorting to efficiently determine the optimal result.\n\n# Approach\n- **Frequency Map :** Create a frequency map to count occurrences of each number in the array.\n\n- **Event List :** For each number, add events for incrementing and decrementing its frequency within the allowed range (k).\n\n- **Sorted Events :** Sort the events and use a TreeSet to maintain the unique event points.\n\n- **Prefix Sum Calculation :** Iterate through the sorted events to calculate the cumulative frequency changes.\n\n- **Maximum Frequency Calculation :** Use the prefix sums to determine the maximum achievable frequency for each number, considering the given number of operations (numOperations).\n\n# Complexity\n- Time complexity: `O(n*logn)`\n\n- Space complexity: `O(n)`\n\n# Code\n```java []\nclass Solution {\n    public int maxFrequency(int[] nums, int k, int numOperations) {\n        int n = nums.length;\n        Map<Integer, Integer> map = new HashMap<>();\n        List<int[]> list = new ArrayList<>();\n        for (int num : nums) {\n            map.put(num, map.getOrDefault(num, 0) + 1);\n            list.add(new int[] { num - k, 1 });\n            list.add(new int[] { num + k + 1, -1 });\n        }\n        TreeSet<Integer> ts = new TreeSet<>();\n        for (int[] l : list) {\n            ts.add(l[0]);\n        }\n        for (int num : map.keySet()) {\n            ts.add(num);\n        }\n        Collections.sort(list, (a, b) -> Integer.compare(a[0], b[0]));\n        int idx = 0;\n        int cur = 0, max = 0;\n        for (int t : ts) {\n            while (idx < list.size() && list.get(idx)[0] <= t) {\n                cur += list.get(idx)[1];\n                idx++;\n            }\n            int val = map.getOrDefault(t, 0);\n            max = Math.max(max, val + Math.min(numOperations, cur - val));\n        }\n        return max;\n    }\n}\n```
['Sliding Window', 'Sorting', 'Hash Function', 'Prefix Sum', 'Java']