Maximum Frequency of an Element After Performing Operations II - Problem

Imagine you have an array of numbers and the power to strategically modify exactly numOperations elements to maximize how many times any single value appears in the final array.

Given an integer array nums and two integers k and numOperations, you must perform exactly numOperations operations. In each operation:

  • Select an index i that hasn't been selected before
  • Add any integer in the range [-k, k] to nums[i]

Goal: Return the maximum possible frequency of any element after performing all operations optimally.

Example: With nums = [1, 4, 5], k = 3, and numOperations = 2, you could modify the first two elements to both become 4, giving you three 4's total (frequency = 3).

Input & Output

example_1.py β€” Basic case with optimal modifications
$ Input: nums = [1, 4, 5], k = 3, numOperations = 2
β€Ί Output: 3
πŸ’‘ Note: We can modify the first element (1) by adding 3 to get 4, and modify the third element (5) by subtracting 1 to get 4. This gives us [4, 4, 4] with frequency 3.
example_2.py β€” Limited operations constraint
$ Input: nums = [1, 4, 5, 2, 8], k = 3, numOperations = 2
β€Ί Output: 3
πŸ’‘ Note: Target value 4: Original count = 1, can modify [1,5,2] (3 elements), but limited to 2 operations. Final frequency = 1 + 2 = 3.
example_3.py β€” Edge case with no modifications needed
$ Input: nums = [5, 5, 5, 5], k = 2, numOperations = 1
β€Ί Output: 4
πŸ’‘ Note: All elements are already the same value (5). No modifications needed, so the maximum frequency is 4.

Constraints

  • 1 ≀ nums.length ≀ 105
  • 1 ≀ nums[i] ≀ 109
  • 0 ≀ k ≀ 109
  • 0 ≀ numOperations ≀ nums.length
  • Each index can be selected at most once

Visualization

Tap to expand
🎨 Gallery Curator's Optimization StrategyOriginal Paintings Collection14528Step 1: Binary Search on Collection SizeTesting sizes: [1] [2] [3] [4] [5] β†’ Binary search finds optimal size efficientlyStep 2: Smart Target Selection (k=3)Target: 4Original: 1 paintingCan modify: 3 paintings[1β†’4, 5β†’4, 2β†’4]Max achievable: 3Target: 5Original: 1 paintingCan modify: 2 paintings[4β†’5, 2β†’5]Max achievable: 3🎯 Result: Maximum collection size = 3Can create 3 identical masterpieces using optimal target selection and 2 modification operations
Understanding the Visualization
1
Smart Range Selection
Instead of testing every possible collection size, use binary search to efficiently narrow down the optimal size
2
Strategic Target Selection
For each collection size, only consider targets that are achievable from existing paintings (original values Β± modification range)
3
Efficient Verification
Quickly count how many paintings can contribute to each target collection, considering both unchanged and modified pieces
Key Takeaway
🎯 Key Insight: Binary search on the answer combined with smart candidate selection transforms a brute force O(rangeΓ—n) solution into an efficient O(n logΒ²n) algorithm by only testing promising targets and frequencies.
Asked in
Google 42 Meta 38 Amazon 31 Microsoft 29 Apple 22
54.2K Views
High Frequency
~25 min Avg. Time
1.8K 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