O(N) | Intuitive | Prefix sum + 4* Sliding Window
\n# Complexity\n- Time complexity: O(5N)\n\n\n# Code\npython3 []\nclass Solution:\n    def maxFrequency(self, nums: List[int], k: int, numOperations: int) -> in
\n# Complexity\n- Time complexity: $$O(5N)$$\n\n\n# Code\n```python3 []\nclass Solution:\n    def maxFrequency(self, nums: List[int], k: int, numOperations: int) -> int:\n        nums = sorted(Counter(nums).items())\n        M = len(nums)\n\n        #1. prefixSum for range count query\n        prefixSum = [nums[0][1]]\n        for i in range(1,M):\n            prefixSum.append(prefixSum[-1]+nums[i][1])\n\n        def count(l,r):\n            return prefixSum[r]-(prefixSum[l-1] if l>0 else 0)\n\n        #2. cal lr reach(within +-k)\u3000of each index\n        lreach = [None for _ in range(M)]\n        rreach = [None for _ in range(M)]\n        l = 0\n        for i in range(M):\n            while nums[i][0]-nums[l][0]>k:\n                l+=1\n            lreach[i]=l\n        r = M-1\n        for i in range(M-1,-1,-1):\n            while nums[r][0]-nums[i][0]>k:\n                r-=1\n            rreach[i]=r\n\n\n        #3. Case 1: iterate EXISTING indicies as pivot, using lr-reach calculated\n        ans = 0\n        for p in range(M):\n            cur = 0\n            cntExSelf = count(lreach[p],rreach[p])-nums[p][1]\n            if cntExSelf<=numOperations:\n                cur = cntExSelf + nums[p][1]\n            else:\n                cur = numOperations + nums[p][1]\n            ans=max(ans,cur)\n\n        #4. Case 2: slide a 2*k window for non-existing pivot\n        l=0\n        for r in range(M):\n            while nums[r][0]-nums[l][0]>2*k:\n                l+=1\n            if r>=l:\n                cnt = count(l,r)\n                if cnt<=numOperations:\n                    ans=max(ans, cnt)\n                else:\n                    ans=max(ans, numOperations)\n\n        return ans\n\n\n\n```
['Python3']