Maximum Absolute Sum of Any Subarray
Real-world version: The Biggest Swing in a Run of AdjustmentsThe statement here is our own: the same inputs, outputs and technique.
A ledger of adjustments reads adjustments. A stretch is any run of neighbouring entries, and the empty stretch counts as one, with a total of nothing.
Return the largest size any stretch's total reaches, where size ignores whether the total came out above or below nothing.
Examples
Example 1
- Input
- adjustments = [4, -1, 2, 1]
- Output
- 6
The whole ledger totals six, and no shorter stretch swings further in either direction.
Example 2
- Input
- adjustments = [-1, -2, -3]
- Output
- 6
Every entry pulls downward, so the whole ledger's total of minus six is the furthest swing and its size is six.
Example 3
- Input
- adjustments = [10000, -10000, 10000]
- Output
- 10000
Either of the two rises on its own swings ten thousand. Taking more entries only cancels part of the swing away.
Constraints
- 1 <= adjustments.length <= 10^5
- -10^4 <= adjustments[i] <= 10^4
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def max_absolute_sum(adjustments: list[int]) -> int:public int maxAbsoluteSum(int[] adjustments)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 15 test cases: 15 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's