We have an array of integers and a budget of k increment operations. Each operation adds 1 to any element we choose. We want to maximize how many elements share the same value after using at most k operations.
We can only increment elements, never decrement them. So to make a group of elements equal, they must all be raised to the value of the largest element in that group. Overshooting past that largest value only wastes operations, so the target is always some value already present in the array.
The problem reduces to: pick a target value equal to one of the array elements, and find how many smaller elements we can afford to raise up to that target within the budget of k.
Sorting makes this tractable. Once the array is sorted, the elements closest in value to a target sit right next to it, and those are the cheapest to raise. The problem becomes finding the longest window in a sorted array where the cost of raising every element to the window's maximum stays within k.
1 <= nums.length <= 10^5 --> We need O(n log n) or better. An O(n^2) approach approaching 10^10 operations would be too slow.1 <= nums[i] <= 10^5 and 1 <= k <= 10^5 --> A window of up to 10^5 elements valued up to 10^5 gives a cost of up to 10^10, which overflows a 32-bit integer. The window sum and cost must use a 64-bit type.Sort the array, then for every possible target element nums[right], count how many elements to its left we can afford to raise up to that value. Iterate backward from right, accumulating the cost of each increment, and stop once the budget is exhausted.
Working backward from the target picks up the cheapest elements first, since in sorted order the elements nearest nums[right] need the fewest increments to reach it. Including those before the farther ones maximizes the count for any fixed budget.
right from 0 to n-1, treat nums[right] as the target value.right, iterate backward from right-1 to 0, accumulating the cost to raise each element to nums[right].k.nums[right]) is a candidate answer.right.Loading animation...
For each target element, this scans backward through the array and recomputes the cost from scratch. Maintaining a running window sum and only moving the left boundary when the budget is exceeded removes that repeated work.
After sorting, the cheapest elements to raise to any target are its immediate neighbors, so the affordable elements for a given target form a contiguous block. That maps directly onto a sliding window over the sorted array.
For a window [left, right], raising every element to nums[right] (the largest in the window) costs nums[right] * windowSize - windowSum. Each element nums[i] needs nums[right] - nums[i] increments to reach the target; summing that over the window gives nums[right] * windowSize minus the sum of the window.
The window expands right one step at a time, adding the new element to the running sum. When the cost exceeds k, it shrinks from the left by removing nums[left] from the sum and advancing left. The largest window size reached is the answer.
Two properties of the sorted array justify the window. First, the optimal group to make equal is always contiguous: for a fixed target, swapping a closer (cheaper) element for a farther (more expensive) one can only raise the cost, so an optimal selection never skips a nearer element to include a farther one. Second, the left boundary never moves backward. When the target grows from nums[right] to nums[right+1], the cost of every element already in the window increases, so any left that was too expensive before is still too expensive, and left can only advance.
Because left only moves forward, each element enters and leaves the window at most once, giving a linear scan after sorting.
left = 0, windowSum = 0, maxFreq = 0.right from 0 to n-1:nums[right] to windowSum.nums[right] * (right - left + 1) - windowSum > k, subtract nums[left] from windowSum and increment left.maxFreq = max(maxFreq, right - left + 1).maxFreq.Loading animation...
The sliding window is already O(n log n), dominated by sorting. The same time bound is reachable from a different angle: fix each right endpoint and binary search for how far left the window can extend, using prefix sums to evaluate each candidate in constant time.
After sorting, for each index right, find the smallest index left such that the cost of making all elements in [left, right] equal to nums[right] is at most k. The window size right - left + 1 for that smallest left is the best frequency achievable with nums[right] as the target.
The cost is nums[right] * (right - left + 1) - sum(nums[left..right]). Precomputing prefix sums makes sum(nums[left..right]) an O(1) lookup. As left moves closer to right, the window holds fewer and cheaper-to-raise elements, so the cost decreases monotonically. That monotonicity allows a binary search for the smallest affordable left.
The approach builds a prefix sum array, then for each right binary searches for the farthest-left position that keeps the cost within k.
Each binary search step evaluates the cost for a candidate left, which needs the sum of nums[left..right]. Recomputing that sum by scanning the window costs O(n) per step, making the search O(n log n) per right endpoint and O(n^2 log n) overall, worse than the brute force. Prefix sums turn each range-sum lookup into O(1), so each binary search step is O(1) and the per-endpoint search is O(log n).
prefix[i] = sum of nums[0..i-1].right from 0 to n-1:left in [0, right] such that nums[right] * (right - left + 1) - (prefix[right+1] - prefix[left]) <= k.right - left + 1.Loading animation...