Python3 | Sliding Window explained
Intuition\nWe can use a sliding window to track the elements that can be modified in order to reach a middle value.\n\nThere are 2 main cases:\n    * the elemen
# Intuition\nWe can use a sliding window to track the elements that can be modified in order to reach a middle value.\n\nThere are 2 main cases:\n    * the element that we want to create is already in the window - in this case, we need to consider all the possible pivots, in order to apply operations only on those elements that have to be modified, so we don\'t waste operations\n    * the element is a value not present in the window, but reachable by applying operations of `+ [-k,k]` on each of the window elements\n\n# Complexity\n- Time complexity:\n$$O(n)$$\n\n# Code\n```python3 []\nclass Solution:\n    def maxFrequency(self, nums: List[int], k: int, numOperations: int) -> int:\n        nums.sort()\n\n        right = 0\n        res, n = 0, len(nums)\n        cnt = Counter()\n\n        # find the longest window, when the target element has an intermediary value between nums[i] and nums[right]\n        for i in range(n):            \n            while right < n and nums[i] + k >= nums[right] - k:\n                cnt[nums[right]] += 1                \n                right += 1\n\n            res = max(res, min(right - i, numOperations))\n            cnt[nums[i]] -= 1\n\n        right = 0\n        mid = 0\n        cnt = Counter()\n\n        # find the longest window, when the target element is inside the window already\n        for i in range(n):         \n            while mid < n and nums[mid] - nums[i] <= k:\n                while right < n and nums[right] - nums[mid] <= k:\n                    cnt[nums[right]] += 1\n                    right += 1\n\n                res = max(res, min(right - i - cnt[nums[mid]], numOperations) + cnt[nums[mid]])\n                mid += 1\n\n            cnt[nums[i]] -= 1\n\n        return res\n```
['Python3']