Maximum Frequency of an Element After Performing Operations I - Problem

You are given an integer array nums and two integers k and numOperations. Your goal is to maximize the frequency of any element in the array after performing exactly numOperations operations.

In each operation, you must:

  • Select an index i that has not been selected in any previous operation
  • Add any integer in the range [-k, k] to nums[i]

The key insight is that you can strategically modify elements to make them converge to the same value, thereby maximizing the frequency of that target value.

Example: If nums = [1, 4, 5], k = 3, and numOperations = 2, you could:

  • Add +2 to nums[0] โ†’ nums becomes [3, 4, 5]
  • Add -1 to nums[1] โ†’ nums becomes [3, 3, 5]

Now the value 3 appears twice, giving us a maximum frequency of 2.

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [1, 4, 5], k = 3, numOperations = 2
โ€บ Output: 2
๐Ÿ’ก Note: We can modify 1 to 4 (add +3) and 5 to 4 (add -1). Now we have [4, 4, 4] with two 4's using 2 operations, giving frequency 2.
example_2.py โ€” No Operations
$ Input: nums = [1, 1, 2, 2, 2], k = 2, numOperations = 0
โ€บ Output: 3
๐Ÿ’ก Note: Without any operations, the maximum frequency is 3 (three 2's already present).
example_3.py โ€” Large K Value
$ Input: nums = [1, 10, 100], k = 50, numOperations = 2
โ€บ Output: 3
๐Ÿ’ก Note: We can convert all numbers to the same value, e.g., convert 1โ†’51 (+50) and 100โ†’51 (-49). All three numbers can become 51.

Visualization

Tap to expand
๐ŸŽฏ Archery Target Optimization14810๐ŸŽฏ Target: 7+6+3-1k = 5: Archers at positions 4, 8, 10 can reach target 7numOperations = 2: Coach 2 closest archers (8โ†’7, 4โ†’7)๐Ÿ† Result: 2 archers hit target 7Original archer at 1 stays put (too far)Final positions: [1, 7, 7, 10]
Understanding the Visualization
1
Survey the Field
Look at where all arrows currently landed (original array values)
2
Identify Targets
Find promising target values that multiple archers can reach
3
Calculate Distances
For each target, measure how far each archer needs to adjust
4
Optimize Coaching
Use your limited coaching sessions on the archers closest to the target
5
Find Best Target
Choose the target that maximizes the number of hits
Key Takeaway
๐ŸŽฏ Key Insight: The optimal target is always reachable from existing positions. We don't need to check impossible targets, just find the sweet spot where the most archers can converge!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n log(max_val))

Binary search on answer * verification cost per candidate

n
2n
โšก Linearithmic
Space Complexity
O(n)

Space for sorting and temporary arrays

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 105
  • 1 โ‰ค nums[i] โ‰ค 109
  • 0 โ‰ค k โ‰ค 109
  • 0 โ‰ค numOperations โ‰ค nums.length
  • Each index can be selected at most once
Asked in
Google 42 Meta 28 Amazon 35 Microsoft 19
23.4K Views
Medium-High Frequency
~18 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen