{x}
blog image

Maximum Absolute Sum of Any Subarray

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

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.

 

Example 1:

Input: nums = [1,-3,2,3,-4]
Output: 5
Explanation: The subarray [2,3] has absolute sum = abs(2+3) = abs(5) = 5.

Example 2:

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 <= 105
  • -104 <= nums[i] <= 104

Solution Explanation: Maximum Absolute Sum of Any Subarray

This problem asks for the maximum absolute sum of any subarray within a given integer array nums. A naive approach would involve checking all possible subarrays, leading to a time complexity of O(n³). However, a more efficient dynamic programming approach can achieve a linear time complexity.

Approach: Dynamic Programming

The solution utilizes dynamic programming to track the maximum and minimum sums encountered so far. Instead of storing the entire arrays for maximum and minimum sums, we only need to keep track of the current maximum (f) and current minimum (g) sums ending at each index.

State Transition:

The core idea is that the maximum (or minimum) sum ending at index i is either the current number nums[i] itself or the current number added to the maximum (or minimum) sum ending at the previous index (i-1). Importantly, if the previous maximum sum is negative, it's beneficial to start a new subarray from nums[i] (resetting to 0). Similarly, if the previous minimum sum is positive, it's beneficial to start a new subarray. This is captured in the state transition equations:

  • f[i] = max(f[i-1], 0) + nums[i] (Maximum sum ending at index i)
  • g[i] = min(g[i-1], 0) + nums[i] (Minimum sum ending at index i)

The max(f[i-1], 0) and min(g[i-1], 0) parts ensure that we reset the sum to 0 if the previous sum is detrimental (negative for max, positive for min).

Algorithm:

  1. Initialize f and g to 0. ans stores the maximum absolute sum encountered.
  2. Iterate through the nums array:
    • Update f using the state transition equation for the maximum sum.
    • Update g using the state transition equation for the minimum sum.
    • Update ans with the maximum of the current ans, f, and abs(g).
  3. Return ans.

Time and Space Complexity:

  • Time Complexity: O(n), where n is the length of the nums array. We iterate through the array only once.
  • Space Complexity: O(1). We only use a few constant extra variables (f, g, ans). We don't need to store arrays for f[i] and g[i] for all i because we only need the previous values to compute the current values.

Code Implementation (Python):

class Solution:
    def maxAbsoluteSum(self, nums: List[int]) -> int:
        f = g = 0
        ans = 0
        for x in nums:
            f = max(f, 0) + x
            g = min(g, 0) + x
            ans = max(ans, f, abs(g))
        return ans
 

The implementations in other languages (Java, C++, Go, TypeScript, Rust) follow the same logic, only differing in syntax and standard library functions. They all maintain the same O(n) time and O(1) space complexity.