Optimized Prefix Sum (Only nums[i]-k & nums[i]+k+1)
\n\n# Code\njava []\nclass Solution {\n    public int maxFrequency(int[] nums, int k, int numOperations) {\n        Arrays.sort(nums);\n        TreeMap<Long,Lon
\n\n# Code\n```java []\nclass Solution {\n    public int maxFrequency(int[] nums, int k, int numOperations) {\n        Arrays.sort(nums);\n        TreeMap<Long,Long> map = new TreeMap<>();\n        HashMap<Long,Long> freq = new HashMap<>(); \n        for(int i=0; i<nums.length; i++) {\n            map.put((long)nums[i]-k,map.getOrDefault((long)nums[i]-k,0l)+1);\n            map.put((long)nums[i]+k+1,map.getOrDefault((long)nums[i]+k+1,0l)-1);\n            map.put((long)nums[i],map.getOrDefault((long)nums[i],0l));\n            freq.put((long)nums[i],freq.getOrDefault((long)nums[i],0l)+1);\n        }\n        long pre = 0;\n        long ans = 1;\n        for(Map.Entry<Long,Long> el : map.entrySet()) {\n            pre = pre+el.getValue();\n            long x = freq.getOrDefault((long)el.getKey(),0l);\n            long curr = Math.min(pre-x,numOperations);\n            ans = Math.max(ans,curr+x);\n        }\n        return (int)ans;\n    }\n}\n```
['Java']