You are given a 0-indexed integer array nums and an integer k.
You can perform the following operation on the array at most k times:
i from the array and increase or decrease nums[i] by 1.The score of the final array is the frequency of the most frequent element in the array.
Return the maximum score you can achieve.
The frequency of an element is the number of occurences of that element in the array.
Example 1:
Input: nums = [1,2,6,4], k = 3 Output: 3 Explanation: We can do the following operations on the array: - Choose i = 0, and increase the value of nums[0] by 1. The resulting array is [2,2,6,4]. - Choose i = 3, and decrease the value of nums[3] by 1. The resulting array is [2,2,6,3]. - Choose i = 3, and decrease the value of nums[3] by 1. The resulting array is [2,2,6,2]. The element 2 is the most frequent in the final array so our score is 3. It can be shown that we cannot achieve a better score.
Example 2:
Input: nums = [1,4,4,2,4], k = 0 Output: 3 Explanation: We cannot apply any operations so our score will be the frequency of the most frequent element in the original array, which is 3.
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 1090 <= k <= 1014When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a collection of numbers and you want to make as many of them as identical as possible by slightly changing their values, up to a limited number of total changes. The brute force approach works by checking every single number in your collection and testing every possible target value to see how many matching numbers you can create.
Here's how the algorithm would work step-by-step:
def max_frequency_score(numbers_collection, allowed_operations):
# Sort numbers to evaluate contiguous elements efficiently for any chosen target value
sorted_numbers = sorted(numbers_collection)
total_elements = len(sorted_numbers)
max_frequency_found = 0
# Test each number in the collection as the potential target value for uniformity
for target_index in range(total_elements):
target_value = sorted_numbers[target_index]
current_operations_used = 0
matching_count = 0
# Expand the window outward to include adjacent numbers and calculate operation cost
for current_index in range(target_index, -1, -1):
cost_to_reach_target = target_value - sorted_numbers[current_index]
if current_operations_used + cost_to_reach_target > allowed_operations:
break
current_operations_used += cost_to_reach_target
matching_count += 1
# Track the maximum frequency achieved across all tested target values
if matching_count > max_frequency_found:
max_frequency_found = matching_count
return max_frequency_foundThe optimal strategy relies on organizing the given numbers from smallest to largest and using a sliding window to find the longest contiguous group of numbers that can be made equal using a limited number of operations. By checking groups of increasing sizes and shifting them across the sorted values, we efficiently determine the maximum possible frequency without testing every single combination.
Here's how the algorithm would work step-by-step:
def max_frequency_score(number_list, operation_budget):
number_list.sort()
list_length = len(number_list)
prefix_sums = [0] * (list_length + 1)
for index in range(list_length):
prefix_sums[index + 1] = prefix_sums[index] + number_list[index]
window_start = 0
max_frequency = 0
for window_end in range(list_length):
median_index = (window_start + window_end) // 2
median_value = number_list[median_index]
left_count = median_index - window_start
left_cost = median_value * left_count - (prefix_sums[median_index] - prefix_sums[window_start])
right_count = window_end - median_index
right_cost = (prefix_sums[window_end + 1] - prefix_sums[median_index + 1]) - median_value * right_count
total_cost = left_cost + right_cost
# Shrink the window from the left if operations exceed the allowed budget.
if total_cost > operation_budget:
window_start += 1
max_frequency = max(max_frequency, window_end - window_start + 1)
return max_frequency| Case | How to Handle |
|---|---|
| Single element array where the array length is 1 | Return a frequency of 1 immediately since zero operations are needed to make a single element equal to itself. |
| All elements are already identical | The sliding window will encompass the entire array and k operations will not be needed, returning the total length. |
| The budget k is 0 | The sliding window correctly limits matches to pre-existing duplicate groups in the sorted array, returning the maximum count of identical adjacent elements. |
| Large k capable of making all elements equal to the maximum value | The sliding window expands to cover the entire array because the total cost to equalize all elements will be less than or equal to k. |
| Array contains negative numbers, zeros, and positive numbers mixed together | Sorting the array normally handles negative values and zeros seamlessly as standard arithmetic distance applies uniformly across the number line. |
| Extremely large numbers causing integer overflow during cost calculation | Prefix sums and cost equations must use 64-bit integers to prevent overflow when accumulating differences over large windows. |
| Maximum constraint inputs with length up to 10 to the 5th power | An O(n log n) sorting step followed by an O(n) sliding window approach ensures execution well within standard time limits. |
| All elements are distinct and spread far apart with a small k | The sliding window shrinks from the left whenever the cost exceeds k, ensuring we always find the optimal contiguous subset of size at least 1. |