Simple Sliding Window Solution
Intuition\n Describe your first thoughts on how to solve this problem. \nSort the array and use sliding window based approach.\n# Approach\n Describe your appro
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSort the array and use sliding window based approach.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWe can either try to get the numbers to some number which may or may not be in the array for that we simply sort the array and see the largest possible sliding window where difference between minimum and maximum is atmost k*2, or we can try to get the numbers to equal some number inside the array this would mean we save up on doing the operation on that number itself, the maximum of these two approach is the answer.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n$$O(n)$$\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n$$O(n)$$\n\n# Code\n```python3 []\nclass Solution:\n    def maxFrequency(self, nums: List[int], k: int, numOperations: int) -> int:\n        # 1st part where we try sliding window of size k*2\n        nums.sort()\n        n=len(nums)\n        l=0\n        k2=k+k\n        res=0\n        for r in range(n):\n            while r>l and nums[r]-nums[l]>k2:\n                l+=1\n            res=max(res,r-l+1)\n            res=min(res,numOperations)\n        # now the second part where we center around some number already in the array\n        l=0\n        r=0\n        freq=[]\n        c=Counter(nums)\n        for i in c:\n            freq.append([i,c[i]])\n        freq.sort()\n        n=len(freq)\n        curr=0\n        for i in range(n):\n            target=freq[i][0]\n            while r<n and freq[r][0]-target<=k:\n                curr+=freq[r][1]\n                r+=1\n            while l<i and target-freq[l][0]>k:\n                curr-=freq[l][1]\n                l+=1\n            opused=min(numOperations,curr-freq[i][1])\n            res=max(res,opused+freq[i][1])\n        \n        return res\n            \n        \n            \n```
['Sliding Window', 'Sorting', 'Python3']