[Java/C++/Python] Two cases
Intuition\n- Sorting: Aorting allows efficient processing of ranges.\n- Sliding window: Helps find suitable ranges of numbers.\n- HashMap frequency counting: Ke
# **Intuition**\n- Sorting: Aorting allows efficient processing of ranges.\n- Sliding window: Helps find suitable ranges of numbers.\n- HashMap frequency counting: Keeps track of element occurrences.\n<br>\n\n\n# **Explanation**\nCase 1: Focusing on Existing Elements\nThis case tries to maximize the frequency of a number\nthat\'s already present in the array.\nRange: `[a - k, a + k]`\n\nCase 2: Creating a New Target Value\nThis case explores the possibility of making several numbers equal to a value\nthat might not initially exist in the array.\nRange: `[a, a + k + k]`\n<br>\n\n# **Complexity**\nTime `O(sort)`\nSpace `O(sort)`\n<br>\n\n```Java [Java]\n    public int maxFrequency(int[] A, int k, int numOperations) {\n        Arrays.sort(A);\n\n        // Case 1\n        Map<Integer, Integer> count = new HashMap<>();\n        int res = 0, i = 0, j = 0, n = A.length;\n        for (int a : A) {\n            while (j < n && A[j] <= a + k) {\n                count.put(A[j], count.getOrDefault(A[j], 0) + 1);\n                j++;\n            }\n            while (i < n && A[i] < a - k) {\n                count.put(A[i], count.get(A[i]) - 1);\n                i++;\n            }\n            int cur = Math.min(j - i, count.getOrDefault(a, 0) + numOperations);\n            res = Math.max(res, cur);\n        }\n\n        // Case 2\n        for (i = 0, j = 0; j < n; j++) {\n            while (A[i] + k + k < A[j]) {\n                i++;\n            }\n            res = Math.max(res, Math.min(j - i + 1, numOperations));\n        }\n        return res;\n    }\n```\n\n```C++ [C++]\n    int maxFrequency(vector<int>& A, int k, int numOperations) {\n        sort(A.begin(), A.end());\n\n        // Case 1\n        unordered_map<int, int> count;\n        int res = 0, i = 0, j = 0, n = A.size();\n        for (int a : A) {\n            while (j < n && A[j] <= a + k) {\n                count[A[j]]++;\n                j++;\n            }\n            while (i < n && A[i] < a - k) {\n                count[A[i]]--;\n                i++;\n            }\n            int cur = min(j - i, count[a] + numOperations);\n            res = max(res, cur);\n        }\n\n        // Case 2\n        \n        for (int i = 0, j = 0; j < n; j++) {\n            while (A[i] + k + k < A[j]) {\n                i++;\n            }\n            res = max(res, min(j - i + 1, numOperations));\n        }\n        return res;\n    }\n```\n\n```py [Python3]\n    def maxFrequency(self, A: List[int], k: int, numOperations: int) -> int:\n        A.sort()\n        n = len(A)\n\n        # case 1\n        count = Counter()\n        res = i = j = 0\n        for a in A:\n            while j < n and A[j] <= a + k:\n                count[A[j]] += 1\n                j += 1\n            while i < n and A[i] < a - k:\n                count[A[i]] -= 1\n                i += 1\n            cur = min(j - i, count[a] + numOperations)\n            res = max(res, cur)\n\n        # case 2\n        i = 0\n        for j in range(n):\n            while A[i] + k + k < A[j]:\n                i += 1\n            res = max(res, min(j - i + 1, numOperations))\n        return res\n```\n\n# More Similar Sliding Window Problems\nHere are some similar sliding window problems.\nAlso find more explanations and discussion.\nGood luck and have fun.\n\n- 3347. [Maximum Frequency of an Element After Performing Operations II](https://leetcode.com/problems/maximum-frequency-of-an-element-after-performing-operations-ii/discuss/6027259/JavaC%2B%2BPython-Two-cases)\n- 2958. [Length of Longest Subarray With at Most K Frequency](https://leetcode.com/problems/length-of-longest-subarray-with-at-most-k-frequency/solutions/4382698/java-c-python-sliding-window/)\n- 2831. [Find the Longest Equal Subarray](https://leetcode.com/problems/find-the-longest-equal-subarray/discuss/3934172/JavaC%2B%2BPython-One-Pass-Sliding-Window-O(n))\n- 2799. [Count Complete Subarrays in an Array](https://leetcode.com/problems/count-complete-subarrays-in-an-array/discuss/3836137/JavaC%2B%2BPython-Sliding-Window-O(n))\n- 2779. [Maximum Beauty of an Array After Applying Operation](https://leetcode.com/problems/maximum-beauty-of-an-array-after-applying-operation/discuss/3771308/JavaC%2B%2BPython-Sliding-Window)\n- 2730. [Find the Longest Semi-Repetitive Substring](https://leetcode.com/problems/find-the-longest-semi-repetitive-substring/discuss/3629621/JavaC%2B%2BPython-Sliding-Window)\n- 2555. [Maximize Win From Two Segments](https://leetcode.com/problems/maximize-win-from-two-segments/discuss/3141449/JavaC%2B%2BPython-DP-%2B-Sliding-Segment-O(n))\n- 2537. [Count the Number of Good Subarrays](https://leetcode.com/problems/count-the-number-of-good-subarrays/discuss/3052559/C%2B%2BPython-Sliding-Window)\n- 2401. [Longest Nice Subarray](https://leetcode.com/problems/longest-nice-subarray/discuss/2527496/Python-Sliding-Window)\n- 2398. [Maximum Number of Robots Within Budget](https://leetcode.com/problems/maximum-number-of-robots-within-budget/discuss/2524838/Python-Sliding-Window-O(n))\n- 2024. [Maximize the Confusion of an Exam](https://leetcode.com/problems/maximize-the-confusion-of-an-exam/discuss/1499049/JavaC%2B%2BPython-Sliding-Window-strict-O(n))\n- 1838. [Frequency of the Most Frequent Element](https://leetcode.com/problems/frequency-of-the-most-frequent-element/discuss/1175090/JavaC%2B%2BPython-Sliding-Window)\n- 1493. [Longest Subarray of 1\'s After Deleting One Element](https://leetcode.com/problems/longest-subarray-of-1s-after-deleting-one-element/discuss/708112/JavaC%2B%2BPython-Sliding-Window-at-most-one-0)\n- 1425. [Constrained Subsequence Sum](https://leetcode.com/problems/constrained-subsequence-sum/discuss/597751/JavaC++Python-O(N)-Decreasing-Deque)\n- 1358. [Number of Substrings Containing All Three Characters](https://leetcode.com/problems/number-of-substrings-containing-all-three-characters/discuss/516977/JavaC++Python-Easy-and-Concise)\n- 1248. [Count Number of Nice Subarrays](https://leetcode.com/problems/count-number-of-nice-subarrays/discuss/419378/JavaC%2B%2BPython-Sliding-Window-atMost(K)-atMost(K-1))\n- 1234. [Replace the Substring for Balanced String](https://leetcode.com/problems/replace-the-substring-for-balanced-string/discuss/408978/javacpython-sliding-window/)\n- 1004. [Max Consecutive Ones III](https://leetcode.com/problems/max-consecutive-ones-iii/discuss/247564/JavaC%2B%2BPython-Sliding-Window)\n-  930. [Binary Subarrays With Sum](https://leetcode.com/problems/binary-subarrays-with-sum/discuss/186683/)\n-  992. [Subarrays with K Different Integers](https://leetcode.com/problems/subarrays-with-k-different-integers/discuss/523136/JavaC%2B%2BPython-Sliding-Window)\n-  904. [Fruit Into Baskets](https://leetcode.com/problems/fruit-into-baskets/discuss/170740/Sliding-Window-for-K-Elements)\n-  862. [Shortest Subarray with Sum at Least K](https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/discuss/143726/C%2B%2BJavaPython-O(N)-Using-Deque)\n-  424. [Longest Repeating Character Replacement](https://leetcode.com/problems/longest-repeating-character-replacement/discuss/278271/JavaC%2B%2BPython-Sliding-Window-just-O(n))\n-  209. [Minimum Size Subarray Sum](https://leetcode.com/problems/minimum-size-subarray-sum/discuss/433123/JavaC++Python-Sliding-Window)\n
[]