> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Maximum Frequency After Subarray Operation

> Tested Python solution for LeetCode 3434 with 18 pytest cases. Generate a practice environment with lcpy.

LeetCode 3434, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Dynamic Programming](/catalog/topics/dynamic-programming), [Greedy](/catalog/topics/greedy), [Enumeration](/catalog/topics/enumeration), [Prefix Sum](/catalog/topics/prefix-sum). [View on LeetCode](https://leetcode.com/problems/maximum-frequency-after-subarray-operation/description/).

Generate this problem as a practice environment: tested reference solution, 18 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 3434   # by problem number
lcpy gen -s maximum_frequency_after_subarray_operation   # by problem name
```

## 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]` where `0 <= i <= j <= n - 1`.
* Select an integer `x` and add `x` to **all** the elements in `nums[i..j]`.

Find the **maximum** frequency of the value `k` after the operation.

### Examples

```
Input: nums = [1,2,3,4,5,6], k = 1
Output: 2
```

**Explanation:** After adding -5 to `nums[2..5]`, 1 has a frequency of 2 in `[1, 2, -2, -1, 0, 1]`.

```
Input: nums = [10,2,3,4,5,5,4,3,2,2], k = 10
Output: 4
```

**Explanation:** After adding 8 to `nums[1..9]`, 10 has a frequency of 4 in `[10, 10, 11, 12, 13, 13, 12, 11, 10, 10]`.

### Constraints

* 1 \<= n == nums.length \<= 10^5
* 1 \<= nums\[i] \<= 50
* 1 \<= k \<= 50

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_frequency_after_subarray_operation/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/maximum_frequency_after_subarray_operation/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(50 * n) ~ O(n); Space: O(1)
    def max_frequency(self, nums: list[int], k: int) -> int:
        base = nums.count(k)
        best_gain = 0
        for target in sorted(set(nums) - {k}):
            # Kadane: +1 for target values (convertible to k), -1 for k values
            # (lost by the operation), 0 otherwise.
            cur = 0
            for num in nums:
                weight = 1 if num == target else (-1 if num == k else 0)
                cur = max(weight, cur + weight)
                best_gain = max(best_gain, cur)
        return base + best_gain
```

## Complexity

| Time | Space |
| - | - |
| O(50 \* n) \~ O(n); Space: O(1) | - |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.