> ## 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 Size Subarray Sum Equals k

> Tested Python solution for LeetCode 325 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 325, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Prefix Sum](/catalog/topics/prefix-sum). [View on LeetCode](https://leetcode.com/problems/maximum-size-subarray-sum-equals-k/description/).

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

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

## Problem

Given an integer array `nums` and an integer `k`, return *the maximum length of a* subarray *that sums to* `k`. If there is not one, return `0` instead.

### Examples

```
Input: nums = [1,-1,5,-2,3], k = 3
Output: 4
Explanation: The subarray [1, -1, 5, -2] sums to 3 and is the longest.
```

```
Input: nums = [-2,-1,2,1], k = 1
Output: 2
Explanation: The subarray [-1, 2] sums to 1 and is the longest.
```

### Constraints

* `1 <= nums.length <= 2 * 10^5`
* `-10^4 <= nums[i] <= 10^4`
* `-10^9 <= k <= 10^9`

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n) — single pass, O(1) hash lookups
    # Space: O(n) — first-occurrence index per prefix sum
    def max_sub_array_len(self, nums: list[int], k: int) -> int:
        first_index = {0: -1}
        prefix_sum = 0
        best = 0
        for i, num in enumerate(nums):
            prefix_sum += num
            if prefix_sum - k in first_index:
                best = max(best, i - first_index[prefix_sum - k])
            if prefix_sum not in first_index:
                first_index[prefix_sum] = i
        return best
```

## Complexity

| Time | Space |
| - | - |
| O(n) — single pass, O(1) hash lookups | O(n) — first-occurrence index per prefix sum |

## Tags


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