Leetcode Problem 1749. Maximum Absolute Sum of Any Subarray

1749. Maximum Absolute Sum of Any Subarray

Leetcode Solutions

Using Kadane's Algorithm to Find Maximum Absolute Subarray Sum

  1. Initialize two variables maxSum and minSum to 0 to store the maximum and minimum subarray sums respectively.
  2. Initialize two variables currentMax and currentMin to 0 to store the current maximum and minimum subarray sums ending at the current index.
  3. Iterate over each element in the array nums. a. Update currentMax by taking the maximum of currentMax + nums[i] and nums[i]. b. Update maxSum by taking the maximum of maxSum and currentMax. c. If currentMax becomes negative, reset it to 0. d. Update currentMin by taking the minimum of currentMin + nums[i] and nums[i]. e. Update minSum by taking the minimum of minSum and currentMin. f. If currentMin becomes positive, reset it to 0.
  4. After the iteration, the maximum absolute sum is the maximum of the absolute values of maxSum and minSum.
  5. Return the maximum absolute sum.
UML Thumbnail

Brute Force Approach to Find Maximum Absolute Subarray Sum

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...