> ## 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 Absolute Sum of Any Subarray

> Tested Python solution for LeetCode 1749 with 23 pytest cases. Generate a practice environment with lcpy.

LeetCode 1749, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Dynamic Programming](/catalog/topics/dynamic-programming). [View on LeetCode](https://leetcode.com/problems/maximum-absolute-sum-of-any-subarray/description/).

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

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

## Problem

You are given an integer array `nums`. The **absolute sum** of a subarray `[nums_l, nums_l+1, ..., nums_r-1, nums_r]` is `abs(nums_l + nums_l+1 + ... + nums_r-1 + nums_r)`.

Return the maximum absolute sum of any (possibly empty) subarray of `nums`.

Note that `abs(x)` is defined as follows:

* If `x` is a negative integer, then `abs(x) = -x`.
* If `x` is a non-negative integer, then `abs(x) = x`.

### Examples

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

**Explanation:** The subarray `[2,3]` has absolute sum = abs(2+3) = abs(5) = 5.

```
Input: nums = [2,-5,1,-4,3,-2]
Output: 8
```

**Explanation:** The subarray `[-5,1,-4]` has absolute sum = abs(-5+1-4) = abs(-8) = 8.

### Constraints

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

## Solution

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

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def max_absolute_sum(self, nums: list[int]) -> int:
        max_sum = 0
        min_sum = 0
        best = 0
        for num in nums:
            max_sum = max(max_sum + num, num)
            min_sum = min(min_sum + num, num)
            best = max(best, max_sum, -min_sum)
        return best
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


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