Maximum Frequency After Subarray Operation - Problem
You are given an array nums of length n. You are also given an integer k.
You perform the following operation on nums once:
- Select a subarray
nums[i..j]where0 <= i <= j <= n - 1 - Select an integer
xand addxto all the elements innums[i..j]
Find the maximum frequency of the value k after the operation.
Input & Output
Example 1 — Basic Transformation
$
Input:
nums = [1,2,4,2], k = 4
›
Output:
3
💡 Note:
Select subarray [1,3] and add x=2. Array becomes [1,4,4,4]. The frequency of 4 is 3.
Example 2 — No Transformation Needed
$
Input:
nums = [1,4,4,2,4], k = 4
›
Output:
4
💡 Note:
Select subarray [3,3] and add x=2. Array becomes [1,4,4,4,4]. The frequency of 4 is 4.
Example 3 — Single Element
$
Input:
nums = [5], k = 3
›
Output:
1
💡 Note:
Select subarray [0,0] and add x=-2. Array becomes [3]. The frequency of 3 is 1.
Constraints
- 1 ≤ nums.length ≤ 105
- -109 ≤ nums[i], k ≤ 109
Visualization
Tap to expand
Understanding the Visualization
1
Input
Array [1,2,4,2] with target k=4
2
Transform
Add x=2 to subarray [1,3]: elements 2→4, 4→6, 2→4
3
Result
Array becomes [1,4,6,4], but better choice gives [1,4,4,4]
Key Takeaway
🎯 Key Insight: Try all meaningful transformations on all subarrays to maximize target frequency
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code