Before attempting this problem, you should be comfortable with:
The operation lets us pick a subarray and add a constant to every element in it. Our goal is to maximize how often some value k appears. The key insight is that we want to convert elements of some value num into k by choosing the right constant. For each candidate num, we try every possible subarray, count how many num values we convert to k, and subtract any k values that get changed to something else within the subarray.
k appears in the array (call it cntK).num from 1 to 50 (excluding k):i:j.num (it becomes k after the operation).cntK when we see k (it changes to something else).cntK + count.class Solution:
def maxFrequency(self, nums: List[int], k: int) -> int:
n = len(nums)
cntK = nums.count(k)
res = cntK
for num in range(1, 51):
if num == k: continue
for i in range(n):
tmp, cnt = cntK, 0
for j in range(i, n):
if nums[j] == num:
cnt += 1
elif nums[j] == k:
cntK -= 1
res = max(res, cnt + cntK)
cntK = tmp
return resThe brute force tries all subarrays, but we can use Kadane's algorithm to find the best subarray in linear time. For a fixed target value num, we want to find a subarray where converting num to k gives the maximum net gain. Treat each num as +1 (we gain a k) and each k as -1 (we lose a k). Kadane's algorithm finds the maximum sum subarray, giving us the best gain for that target.
k appears in the array (call it cntK).i from 1 to 50 (excluding k):cnt = 0.1 if the element equals i.1 if the element equals k.cnt to 0 if it goes negative (Kadane's reset).cntK + cnt.class Solution:
def maxFrequency(self, nums: List[int], k: int) -> int:
cntK = nums.count(k)
res = 0
for i in range(1, 51):
if i == k:
continue
cnt = 0
for num in nums:
if num == i:
cnt += 1
if num == k:
cnt -= 1
cnt = max(cnt, 0)
res = max(res, cntK + cnt)
return resWe can process all target values simultaneously in a single pass. For each number, we track the best running sum ending at the current position. When we see a number, its count can either extend from its previous best or start fresh from k's position (since we could start our subarray here). The maximum difference between any count and k's count represents the best gain achievable.
cnt where cnt[num] represents the best running sum ending at the current position for converting num to k.cnt[num] = max(cnt[num], cnt[k]) + 1.cnt[num] - cnt[k].cnt[k] + res, where res is the maximum gain found.The operation adds a constant to every element in a chosen subarray. A common mistake is to think you can selectively modify some elements within the subarray while leaving others unchanged. Every element in the range [l, r] gets the same constant added. This means if you have both k and num values in your chosen subarray, converting num to k will simultaneously change existing k values to something else.
When you apply the operation to a subarray to convert num values to k, any existing k values within that subarray become k + constant, which is no longer k. The net gain is (count of num in subarray) - (count of k in subarray). Solutions that only count the gains without subtracting the losses will produce incorrect results.
When iterating through all possible target values (1 to 50), each target requires its own independent Kadane's algorithm pass. Failing to reset the running count between targets will carry over state from previous iterations, leading to incorrect maximum subarray calculations. Initialize cnt = 0 at the start of each target value's iteration.
The operation is optional. Sometimes the best strategy is to not perform any operation at all, especially when k already appears frequently. The answer should always be at least count(k) in the original array. Some solutions focus so heavily on finding the best subarray that they forget to compare against the base case of leaving the array unchanged.
Kadane's algorithm finds the maximum sum subarray, but the classic version requires at least one element. In this problem, an empty subarray (doing nothing to convert any num to k) is valid and contributes 0 to the gain. Solutions must allow the running sum to reset to 0 when it goes negative, representing the choice to not include those elements in the operation subarray.