The frequency of an element is the number of times it occurs in an array.
You are given an integer array nums and an integer k. In one operation, you can choose an index of nums and increment the element at that index by 1.
Return the maximum possible frequency of an element after performing at most k operations.
Example 1:
Input: nums = [1,2,4], k = 5 Output: 3 Explanation: Increment the first element three times and the second element two times to make nums = [4,4,4]. 4 has a frequency of 3.
Example 2:
Input: nums = [1,4,8,13], k = 5 Output: 2 Explanation: There are multiple optimal solutions: - Increment the first element three times to make nums = [4,4,8,13]. 4 has a frequency of 2. - Increment the second element four times to make nums = [1,8,8,13]. 8 has a frequency of 2. - Increment the third element five times to make nums = [1,4,13,13]. 13 has a frequency of 2.
Example 3:
Input: nums = [3,9,6], k = 2 Output: 1
Constraints:
1 <= nums.length <= 1051 <= nums[i] <= 1051 <= k <= 105When 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 items of various sizes and you want to make as many of them equal as possible by boosting their values within a limited budget of total increases. The brute force approach systematically tries every single item in the collection as the target value and tests how many other items can be raised to match it using your available budget.
Here's how the algorithm would work step-by-step:
def max_frequency(numbers_list, budget_limit):
# Sort numbers to easily check elements smaller than the target in ascending order
sorted_numbers = sorted(numbers_list)
maximum_frequency = 0
# Iterate through every item to test it as the potential target value
for target_index in range(len(sorted_numbers)):
target_value = sorted_numbers[target_index]
current_budget = budget_limit
current_frequency = 0
# Scan backward from the target to find how many smaller elements we can boost
for scan_index in range(target_index, -1, -1):
cost_to_increase = target_value - sorted_numbers[scan_index]
if current_budget >= cost_to_increase:
current_budget -= cost_to_increase
current_frequency += 1
else:
break
# Keep track of the highest count achieved across all tested target values
if current_frequency > maximum_frequency:
maximum_frequency = current_frequency
return maximum_frequencyTo find out how many times we can make the most frequent item appear by boosting smaller items, the best strategy is to sort everything from smallest to largest and examine groups of items side-by-side. By using a flexible window that expands when we have enough resources to boost everyone up to the highest value in the group, and shrinks from the back when costs get too high, we can efficiently track the largest possible group.
Here's how the algorithm would work step-by-step:
def max_frequency(nums, k):
nums.sort()
left_pointer = 0
current_sum = 0
max_freq = 0
# Sorting allows us to evaluate contiguous elements that can be efficiently incremented.
for right_pointer in range(len(nums)):
current_sum += nums[right_pointer]
# If increments exceed budget, shrink window from left to maintain valid operations.
while (right_pointer - left_pointer + 1) * nums[right_pointer] - current_sum > k:
current_sum -= nums[left_pointer]
left_pointer += 1
# Track the largest valid window size found during our linear scan.
max_freq = max(max_freq, right_pointer - left_pointer + 1)
return max_freq| Case | How to Handle |
|---|---|
| Array with a single element | The sliding window correctly evaluates a window size of one and returns a frequency of one. |
| k is equal to zero | With no increments allowed, the window size collapses and returns the count of the most frequent duplicate element. |
| Array with all identical elements | Sorting has no effect and the sliding window expands to cover the entire array length. |
| k is large enough to make all elements equal to the maximum element | The window expands to encompass the entire sorted array because the cost condition is always satisfied. |
| Negative numbers and zeros in the array | Sorting correctly orders negative numbers, zeros, and positives, ensuring the distance math holds true. |
| Large inputs with maximum constraints causing integer overflow during window cost calculations | Using 64-bit integers for the sum and window calculations prevents arithmetic overflow during multiplication. |
| Maximum sized inputs where O(n squared) naive approaches would Time Out | The O(n log n) sorting plus O(n) sliding window approach ensures efficient scaling for large datasets. |