C#, Sliding Window, O(n)
Intuition\nThe problem is akin to finding the largest contiguous subarray in a sorted array where the total cost of making all elements equal does not exceed a 
# Intuition\nThe problem is akin to finding the largest contiguous subarray in a sorted array where the total cost of making all elements equal does not exceed a given limit k. The intuition here is to use a sliding window approach, adjusting the window size and the target value (modeIndex) within it to minimize the cost.\n\n# Approach\n- Sort the Array: First, sort the array to bring similar values close together, which is crucial for efficient cost calculation.\n- Sliding Window: Use a sliding window (defined by tail and head) to explore different subarrays.\n- Cost Calculation and Adjustment: Calculate the cost of making all elements in the window equal to the element at modeIndex. Adjust modeIndex within the window to minimize this cost.\n- Optimizing Window Size: Expand the window by moving head and adjust modeIndex to keep the cost within k. If the cost exceeds k, shrink the window from tail.\n- Finding the Best Window: Track the maximum window size where the total cost is within the limit k.\n\n# Complexity\n\n1. **Sorting Complexity**: The initial sort of the array typically has a time complexity of **O(n log n)**, where **n** is the number of elements in the array. This is based on the average complexity of efficient sorting algorithms like mergesort or quicksort, which are commonly used in standard library sort functions. Using radixsort could reduce overall complexity to approach **O(n)**.\n\n2. **Sliding Window Complexity**: After sorting, the sliding window mechanism, where the `modeIndex` is incremented globally and the window is adjusted, contributes an additional **O(n)** complexity, as each element is considered once in the process.\n\n3. **Overall Time Complexity**: The overall time complexity of the algorithm is dominated by the sorting step, making it **O(n log n)**. While the sliding window adds to the total computation, its linear complexity does not exceed that of the sorting step.\n\n4. **Space Complexity**: The space complexity remains **O(1)** if the sort is done in-place (which is typically the case in many programming environments).\n\n# Code\n```\npublic class Solution {\n    private long CalculateDeltaCost(int[] nums, int tail, int head, int curIndex, int newIndex) {\n        long diff = nums[newIndex] - nums[curIndex];\n        long costAdd = diff * (curIndex - tail + 1);\n        long costSub = diff * (head - newIndex + 1);\n\n        return costAdd - costSub;\n    }\n\n    public int MaxFrequencyScore(int[] nums, long k) {\n        Array.Sort(nums);\n        int tail = 0, head = -1, best = 0, modeIndex = 0;\n        long currentCost = 0;\n\n        for (head = 0; head < nums.Length; head++) {\n            // Update cost for the new head\n            currentCost += Math.Abs(nums[head] - nums[modeIndex]);\n\n            // Steering the mode to reduce the cost\n            while (modeIndex < head) {\n                long deltaCost = CalculateDeltaCost(nums, tail, head, modeIndex, modeIndex + 1);\n                if (deltaCost <= 0) {\n                    currentCost += deltaCost;\n                    modeIndex++;\n                } else {\n                    break;\n                }\n            }\n \n            // Update the best window size\n            if (currentCost <= k) {\n                best = Math.Max(best, head - tail + 1);\n            }\n            // Adjusting tail when the cost exceeds k\n            else if (tail < head) {\n                currentCost -= Math.Abs(nums[modeIndex] - nums[tail]);\n                tail++;\n            }\n            else\n                break;\n        }\n\n        return best;\n    }\n}\n\n```
['C#']