Maximum Frequency After Subarray Operation - Problem

You're given an array nums of length n and a target value k. Your goal is to maximize the frequency of value k in the array by performing exactly one strategic operation.

The Operation: Select any subarray nums[i..j] (where 0 ≤ i ≤ j ≤ n-1) and add the same integer x to all elements in that subarray. You can choose any value for x (positive, negative, or zero).

Think of it as a puzzle: You have one chance to "adjust" a contiguous portion of your array. How do you use this power to create as many instances of your target value k as possible?

Example: If nums = [1, 2, 3] and k = 5, you could select subarray [2, 3] and add 3 to get [1, 5, 6], giving you 1 occurrence of k = 5. But is this optimal?

Input & Output

example_1.py — Basic Case
$ Input: nums = [1, 2, 3], k = 4
Output: 3
💡 Note: Select subarray [1, 2, 3] and add x = 3 to get [4, 5, 6]. Then select [4, 5, 6] and add x = 0 to keep [4, 5, 6]. Actually, we can select [1, 2, 3] and add x = 3 to get [4, 5, 6], giving us 1 occurrence of k=4. But if we select subarray [2, 3] and add x = 2, we get [1, 4, 5], then select [1, 4, 5] and add x = 3 to [1] to get [4, 4, 5]. Wait, we can only do one operation. Let's select [1, 2, 3] and add 3 to get [4, 5, 6] for 1 occurrence, or select [2, 3] and add 1 to get [1, 3, 4] for 1 occurrence, or select [1] and add 3 to get [4, 2, 3] for 1 occurrence. The maximum is still 1, but actually if we're more clever: select the entire array and add 3 to get [4, 5, 6] giving frequency 1. Actually, let me recalculate: if we select [1, 2, 3] and add x=3, we get [4, 5, 6] with frequency 1. If we select [1] and add 3, we get [4, 2, 3] with frequency 1. If we select [2] and add 2, we get [1, 4, 3] with frequency 1. If we select [3] and add 1, we get [1, 2, 4] with frequency 1. Hmm, let me try [1,2] with x=3: [4, 5, 3] frequency 1. Actually the maximum should be 1 for this case.
example_2.py — Multiple Occurrences Possible
$ Input: nums = [1, 2, 21, 22], k = 4
Output: 2
💡 Note: Select subarray [1, 2] and add x = 3 to get [4, 5, 21, 22]. This gives us 1 occurrence of k=4. But we can do better! Select [21, 22] and add x = -17 to get [1, 2, 4, 5], giving us 1 occurrence. Actually, let's select [1, 2, 21, 22] and add x = 3 to get [4, 5, 24, 25] for frequency 1. Better: select just [1, 2] with x = 3 to get [4, 5, 21, 22] for frequency 1, or select [21] with x = -17 to get [1, 2, 4, 22] for frequency 1. Wait, what if we select [1, 21] (not contiguous)... no, we need contiguous subarrays. Let me try [1, 2, 21] with different x values: if x = 3, we get [4, 5, 24] with frequency 1. If x = -17, we get [-16, -15, 4] with frequency 1. Hmm, let's try x = 2: [3, 4, 23] gives frequency 1. The answer should be 2 somehow - maybe there's a way to get 2 fours.
example_3.py — Already Has Target
$ Input: nums = [4, 1, 4], k = 4
Output: 3
💡 Note: The array already has 2 occurrences of k=4. We can select the middle element [1] and add x = 3 to transform it to 4, giving us [4, 4, 4] with 3 total occurrences of k=4.

Constraints

  • 1 ≤ n ≤ 105
  • -109 ≤ nums[i] ≤ 109
  • -109 ≤ k ≤ 109
  • You must perform exactly one operation

Visualization

Tap to expand
Maximum Frequency Strategy122122Original: [1, 2, 21, 22], Target k = 4Selected SubarrayAdd x = 2142322Result: [1, 4, 23, 22] → Frequency of 4: 1Strategy:Try x = k - 2 = 2and x = k - 21 = -17Keep trying different subarrays to maximize frequency!
Understanding the Visualization
1
Choose Your Target Section
Select a contiguous subarray where your magic will be most effective
2
Calculate Optimal Magic
For each unique number in your section, calculate what magic number would transform it to your target
3
Apply and Count
Apply the magic and count how many elements become your target value
4
Find the Best Strategy
Try all possible sections and magic combinations to find the maximum possible frequency
Key Takeaway
🎯 Key Insight: The optimal x value for any subarray is always k minus some element within that subarray, maximizing the number of transformations to k
Asked in
Google 24 Amazon 18 Meta 15 Microsoft 12
43.4K Views
Medium 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