DSA problem
Maximum Absolute Sum of Any Subarray
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. The maximum absolute sum is simply the larger of the maximum subarray sum and the magnitude of the minimum subarray sum.
Examples
Example 1
Input:
nums = [1,-3,2,3,-4]Output:
5Explanation: The subarray [2,3] has absolute sum abs(2 + 3) = 5.
Example 2
Input:
nums = [2,-5,1,-4,3,-2]Output:
8Explanation: The subarray [-5,1,-4] has absolute sum abs(-5 + 1 + -4) = 8.
Example 3
Input:
nums = [4,-1,-1,4]Output:
6Explanation: The whole array [4,-1,-1,4] has absolute sum abs(6) = 6.
Constraints
- 1 <= nums.length <= 10^5
- -10^4 <= nums[i] <= 10^4
3 hints and a full solution walkthrough with step-by-step visualizations are available in the practice editor.