Java Clean Solution 9/11/2024
Complexity\n- Time complexity:O(Sorting + n)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity:O(n)\n Add your space complexity here, e.g. O(n) 
# Complexity\n- Time complexity:$$O(Sorting + n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(n)$$\n<!-- Add your space complexity here, e.g. $$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\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\n            list.add(new int[] { num - k, 1 });\n            list.add(new int[] { num + k + 1, -1 });\n        }\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\n        // Sorting\n        Collections.sort(list, (a, b) -> Integer.compare(a[0], b[0]));\n\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\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```
['Array', 'Hash Table', 'Math', 'Sorting', 'Hash Function', 'C++', 'Java']