#1749

Maximum Absolute Sum of Any Subarray

Medium Kadane max & min

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Return the largest absolute value of any subarray sum (an empty subarray counts as 0).

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: the largest |sum| of any piece.

Why this works: the largest absolute value is either the most positive sum or the most negative sum; run Kadane for both.

Example: [1, -3, 2, 3, -4]

best positive: 2 + 3 = 5most negative: -4answer 5

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Taking the absolute value of each element first — that changes the problem.

Solve on LeetCode ↗
Complexity
  • Time: O(n).
  • Space: O(1).
Topic: Kadane, Dutch Flag & Moore's Voting
🎬 Episodes 11–13: video coming soon