🔍 Sliding Window Magic: Maximize Frequency with 2 Types of Ranges: k & 2k ✨ || Clear Code in Go 🚀
\uD83D\uDD11 Key Intuition and Edge Cases\n\n#### Key Observations:\n- Sliding Window of k is used to maximize the frequency of an existing element in the array
## \uD83D\uDD11 Key Intuition and Edge Cases\n\n#### Key Observations:\n- __Sliding Window__ of $$k$$ is used to maximize the frequency of an existing element in the array.\n- __Sliding Window__ of $$2k$$ is used when we need to create a new frequent element from existing elements.\n- __Precomputing Frequencies__ using the $$freqs$$ array allows us to efficiently track contiguous occurrences of elements, reducing redundant operations.\n- By combining these techniques, we maximize the frequency while staying within the operation limits.\n\n#### Edge Cases:\n- $$numOperations == 0$$: No changes allowed; return the frequency of the most common element in nums.\n- $$k == 0$$: No range for changes; behavior depends entirely on initial frequencies.\n- _All elements are the same_: Return the array length directly.\n\n\n## \uD83E\uDDE0 Approach Breakdown \u2013 Step by Step\n\nWe have two main cases to consider when trying to maximize the frequency of any element in the array after performing the operations.\n\n#### Case 1: The Maximum Frequency Element is Already in the Array \uD83C\uDFAF\n\n1. What This Means:\n    - In this scenario, the element that has the highest frequency is already present in the array. Our goal is to increase the frequency of this element using the allowed operations.\n2. Sliding Window Technique with Radius $$k$$:\n    - We can use the sliding window technique to check a range of elements that can potentially become the most frequent element.\n    - We slide a window of size $$k$$ around each element in the array.\n    - Why $$k$$? Because the allowed operation adds values in the range $$[-k, k]$$ to the elements in the array. So, any element within this range can be transformed into the current element using one operation.\n3. Using the $$freqs$$ Array:\n    - The $$freqs$$ array helps track how many times each element appears consecutively in the array.\n    - This helps us quickly identify the frequency of adjacent elements without recalculating it from scratch.\n    - If a certain number already appears frequently in a group of contiguous elements, we don\u2019t need to use operations to increase its count; it\u2019s already a candidate for the most frequent element.\n\n4. How We Maximize Frequency:\n    - For each element in the array, we check if we can extend its range of contiguous elements (using the sliding window) within the k bounds.\n     - We calculate the number of operations required to turn other numbers within the window into this element and update the maximum frequency accordingly.\n\n### Case 2: The Maximum Frequency Element Isn\'t in the Array \uD83D\uDE80\n\n1. What This Means:\n    - In this case, the element with the maximum frequency does not already exist in the array. Instead, we are trying to create a new "most frequent" element by adjusting elements from the array.\n\n2. Sliding Window Technique with Radius $$2k$$:\n    - Since we are now trying to make an element that doesn\u2019t exist in the array, we expand our window size to 2k.\n    - The reason we need a 2k window is that we need to cover both the potential range of existing elements (with a k margin on each side) and still be able to transform them into the new frequent element.\n\n3. Maximizing Frequency with the $$2k$$ Window:\n    - The sliding window starts with one side aligned to an element in the array.\n    - As we move the other side of the window, we check if the difference between the numbers within the window is less than or equal to 2k. If it is, we can potentially transform all these elements into a new frequent element using our allowed operations.\n\n\n# Complexity\n- Time complexity:\n    - Sorting the array takes O(n log n).\n    - Sliding window iterations are linear, making them O(n).\n    - Total: O(n log n).\n\n- Space complexity:\n    - Storing frequency array: O(n).\n    - Total: O(n).\n    - \n# Code\n```golang []\nimport "slices"\n\n// Helper function to compute initial frequencies of contiguous elements\nfunc calcFreqs(nums []int) []int {\n    freqs := make([]int, len(nums))\n    for i := 0; i < len(nums); i++ {\n        k := i\n        // Count the range of contiguous elements\n        for ; k < len(nums) && nums[k] == nums[i]; k++ {}\n        // Assign the frequency to all elements in this range\n        for j := i; j < k; j++ {\n            freqs[j] = k - i\n        }\n        // Skip to the next unique element\n        i = k - 1\n    }\n    return freqs\n}\n\n// Main function to maximize the frequency\nfunc maxFrequency(nums []int, k int, numOperations int) int {\n    // Sort the array for easier range-based calculations\n    slices.Sort(nums)\n    \n    // Compute initial frequencies of contiguous elements\n    freqs := calcFreqs(nums)\n    var maxFreq int\n\n    // First sliding window to handle operations within k range\n    l, r := 0, 1\n    for i := range nums {\n        // Expand the range to include numbers within k distance\n        for ; r < len(nums) && nums[r] <= nums[i]+k; r++ {}\n        // Shrink the left boundary if it goes beyond k\n        for ; nums[l] < nums[i]-k; l++ {}\n        // Calculate maximum frequency using operations\n        maxFreq = max(freqs[i] + min(r-l-freqs[i], numOperations), maxFreq)\n    }\n\n    // Second sliding window to consider combining numbers within 2k range\n    l, r = 0, 1\n    for ; l < len(nums); l++ {\n        for r < len(nums) && nums[r]-nums[l] <= 2*k {\n            r++\n        }\n        maxFreq = max(min(r-l, numOperations), maxFreq)\n    }\n\n    return maxFreq\n}\n\n// Helper function to compute the absolute value\nfunc abs(x int) int {\n    if x < 0 {\n        return -x\n    }\n    return x\n}\n\n// Helper function to find the maximum of two integers\nfunc max(a, b int) int {\n    if a > b {\n        return a\n    }\n    return b\n}\n\n// Helper function to find the minimum of two integers\nfunc min(a, b int) int {\n    if a < b {\n        return a\n    }\n    return b\n}\n```
['Array', 'Sliding Window', 'Go']