Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find maximum absolute sum of any subarray in Python
Sometimes we need to find the maximum absolute sum of any subarray within an array. This problem extends Kadane's algorithm by considering both maximum and minimum subarray sums, as the absolute value of a negative sum might be larger than the positive maximum.
Problem Understanding
Given an array nums, we need to find the maximum absolute sum of any contiguous subarray. The absolute sum of a subarray [nums_l, nums_l+1, ..., nums_r] is |nums_l + nums_l+1 + ... + nums_r|.
For example, if nums = [2, -4, -3, 2, -6], the subarray [2, -4, -3, 2] has sum |2 + (-4) + (-3) + 2| = |-3| = 3, but we need to check all possibilities.
Algorithm Approach
We use a modified Kadane's algorithm that finds both:
Maximum subarray sum (positive direction)
Minimum subarray sum (negative direction)
The answer is the maximum absolute value between these two sums.
Implementation
def solve(nums):
n = len(nums)
ans = 0
temp = 0
# Find maximum subarray sum (Kadane's algorithm)
for i in range(n):
if temp < 0:
temp = 0
temp = temp + nums[i]
ans = max(ans, abs(temp))
# Find minimum subarray sum (modified Kadane's)
temp = 0
for i in range(n):
if temp > 0:
temp = 0
temp = temp + nums[i]
ans = max(ans, abs(temp))
return ans
nums = [2, -4, -3, 2, -6]
result = solve(nums)
print(f"Input: {nums}")
print(f"Maximum absolute subarray sum: {result}")
Input: [2, -4, -3, 2, -6] Maximum absolute subarray sum: 11
How It Works
The algorithm works in two passes:
First pass: Finds the maximum positive subarray sum using standard Kadane's algorithm
Second pass: Finds the minimum negative subarray sum by resetting when the running sum becomes positive
In our example, the second pass finds the subarray [-4, -3, 2, -6] with sum -11, giving us |-11| = 11.
Step-by-Step Trace
def solve_with_trace(nums):
print(f"Array: {nums}")
n = len(nums)
ans = 0
temp = 0
print("\nFirst pass (maximum subarray sum):")
for i in range(n):
if temp < 0:
temp = 0
temp = temp + nums[i]
ans = max(ans, abs(temp))
print(f" i={i}, nums[i]={nums[i]}, temp={temp}, ans={ans}")
print(f"After first pass: ans = {ans}")
temp = 0
print("\nSecond pass (minimum subarray sum):")
for i in range(n):
if temp > 0:
temp = 0
temp = temp + nums[i]
ans = max(ans, abs(temp))
print(f" i={i}, nums[i]={nums[i]}, temp={temp}, ans={ans}")
print(f"Final answer: {ans}")
return ans
nums = [2, -4, -3, 2, -6]
solve_with_trace(nums)
Array: [2, -4, -3, 2, -6] First pass (maximum subarray sum): i=0, nums[i]=2, temp=2, ans=2 i=1, nums[i]=-4, temp=-2, ans=2 i=2, nums[i]=-3, temp=-5, ans=5 i=3, nums[i]=2, temp=-3, ans=5 i=4, nums[i]=-6, temp=-9, ans=9 After first pass: ans = 9 Second pass (minimum subarray sum): i=0, nums[i]=2, temp=2, ans=9 i=1, nums[i]=-4, temp=-4, ans=9 i=2, nums[i]=-3, temp=-7, ans=9 i=3, nums[i]=2, temp=-5, ans=9 i=4, nums[i]=-6, temp=-11, ans=11 Final answer: 11
Conclusion
This algorithm efficiently finds the maximum absolute subarray sum in O(n) time by running two modified versions of Kadane's algorithm. The key insight is considering both maximum positive and minimum negative subarray sums to find the largest absolute value.
---