2 windows
Intuition\n Describe your first thoughts on how to solve this problem. \n\n\n\n# Approach\n\n1. Sort numbers.\n\n2. Create window around first number (with valu
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n\n\n# Approach\n\n1. Sort numbers.\n\n2. Create window around first number (with values from ```number - k``` to  ```number + k```). Count how many times current number appears in window. Other numbers need operation so count them only up to ```numOperations```. Move window to next number.\n\n(this step covers case with many repeating numbers)\n\n3. Similar to step 2. Create second window around numbers + k. \n\n(this step covers case with optimal operations)\n\n# Complexity\n- Time complexity: O(2n)\n\n- Space complexity: O(n)\n\n# Code\n```csharp []\npublic class Solution {   \n    public int MaxFrequency(int[] nums, int k, int numOperations) {\n        int max = 0;\n        Dictionary<int, int> counts = nums.GroupBy(n => n).ToDictionary(g => g.Key, g => g.Count());\n        Array.Sort(nums);\n        int last = nums.Length - 1;\n\n        for (int j = 0; j < 2; j++) {\n            int first = 0;\n            int next = 0;\n\n            for (int i = 0; i < nums.Length; i++) {\n                int num = nums[i] + j * k;\n\n                int firstK = num - k;\n                while (first < last && nums[first] < firstK) first++;\n\n                int nextK = num + k;\n                while (next < last && nums[next] <= nextK) next++;\n                if (nums[next] > nextK) next--;\n\n                int newMax = next - first + 1;\n                if (counts.ContainsKey(num)) {\n                    newMax -= counts[num];\n\n                    max = Math.Max(max, Math.Min(numOperations, newMax) + counts[num]);\n                } else {\n                    max = Math.Max(max, Math.Min(numOperations, newMax));\n                }\n            }\n        }\n\n        return max;\n    }\n}\n```
['C#']