Imagine you have an array of numbers and a limited budget of operations to make elements more frequent. The frequency of an element is simply how many times it appears in the array.
You're given an integer array nums and an integer k representing your operation budget. In each operation, you can increment any element by 1. Your goal is to maximize the frequency of any single element after performing at most k operations.
Key insight: You can only increase elements (never decrease), so the optimal strategy involves picking a target value and incrementing smaller elements to match it.
Example: Given [1,2,4] and k=5, you could turn this into [4,4,4] using 5 operations: increment first element 3 times (1โ4) and second element 2 times (2โ4), achieving a frequency of 3.
Input & Output
Visualization
Time & Space Complexity
For each of n elements as target, we potentially scan all n elements to calculate cost
Only using constant extra space for variables
Constraints
- 1 โค nums.length โค 105
- 1 โค nums[i] โค 105
- 1 โค k โค 109
- Important: You can only increment elements, never decrement