Maximum Frequency of an Element After Performing Operations II - Problem
You are given an integer array nums and two integers k and numOperations.
You must perform an operation exactly numOperations times on nums, where in each operation you:
- Select an index
ithat was not selected in any previous operations - Add an integer in the range
[-k, k]tonums[i]
Return the maximum possible frequency of any element in nums after performing the operations.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [2,4,6], k = 3, numOperations = 2
›
Output:
3
💡 Note:
Choose target value 4. Modify nums[0]: 2→4 and nums[2]: 6→4. Result: [4,4,4] with frequency 3.
Example 2 — Limited Operations
$
Input:
nums = [1,4,5], k = 1, numOperations = 2
›
Output:
2
💡 Note:
Choose target value 4. Only nums[2] can be modified: 5→4 (since |1-4|=3 > k=1). Result: [1,4,4] with frequency 2.
Example 3 — No Operations Needed
$
Input:
nums = [3,3,3], k = 1, numOperations = 1
›
Output:
3
💡 Note:
All elements are already equal to 3. No operations needed, frequency is already 3.
Constraints
- 1 ≤ nums.length ≤ 105
- 1 ≤ nums[i] ≤ 109
- 0 ≤ k ≤ 109
- 0 ≤ numOperations ≤ nums.length
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code