VOOZH about

URL: https://www.geeksforgeeks.org/dsa/maximum-absolute-difference-between-sum-of-two-contiguous-sub-arrays/

⇱ Maximum absolute diff of sum of two contiguous Subarrays - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Maximum absolute diff of sum of two contiguous Subarrays

Last Updated : 12 Jun, 2026

Given an array arr[] of integers, find two non-overlapping contiguous sub-arrays such that the absolute difference between the sum of the two sub-arrays is maximum.

Examples:

Input: arr[] = {-2, -3, 4, -1, -2, 1, 5, -3}
Output: 12
Explanation: One possible pair of non-overlapping subarrays is: {-2, -3} and {4, -1, -2, 1, 5}. Their sums are -5 and 7 respectively, so the absolute difference is: |(-5) - 7| = 12

Input: arr[] = {2, -1, -2, 1, -4, 2, 8}
Output: 16
Explanation: One possible pair of non-overlapping subarrays is:{-1, -2, 1, -4} and {2, 8}. Their sums are -6 and 10 respectively, so the absolute difference is: |(-6) - 10| = 16

[Naive Approach] Try All Subarray Pairs - O(n4) Time and O(1) Space

The idea of this approach is to generate every possible contiguous subarray and compare it with every other contiguous subarray that does not overlap with it.
For each valid pair, calculate the sum of both subarrays and update the maximum absolute difference.


Output
12

[Expected Approach] Left and Right Max and Min Arrays - O(n) Time and O(n) Space

The idea of this approach is to split the array at every possible position and find the maximum absolute difference between a subarray on the left side and a subarray on the right side.

To maximize the absolute difference, we only need to consider:

  • The maximum subarray sum on one side and the minimum subarray sum on the other side.
  • The minimum subarray sum on one side and the maximum subarray sum on the other side.

For every index i, we maintain 4 values:

  • leftmax[i] = Maximum subarray sum in arr[0...i],
  • leftmin[i] = Minimum subarray sum in arr[0...i],
  • rightmax[i] = Maximum subarray sum in arr[i...n-1]
  • rightmin[i] = Minimum subarray sum in arr[i...n-1].

These arrays can be built in O(n) time using Kadane's Algorithm. The answer for every partition after index i is obtained using: abs(leftmax[i] - rightmin[i + 1]) or abs(leftmin[i] - rightmax[i + 1])
Taking the maximum value over all partition points gives the final answer.

Consider: arr[] = [-2, -3, 4, -1, -2, 1, 5, -3]
After preprocessing,

  • leftmax[] = {-2, -2, 4, 4, 4, 4, 7, 7}
  • leftmin[] = {-2, -5, -5, -5, -5, -5, -5, -5}
  • rightmax[] = {7, 7, 7, 6, 6, 6, 5, -3}
  • rightmin[] = {-5, -3, -3, -3, -3, -3, -3, -3}.

Now consider the partition after index 1: Left part = {-2, -3} and Right part = {4, -1, -2, 1, 5, -3}
Compute the two possible answers:

abs(leftmax[1] - rightmin[2]) = abs(-2 - (-3)) = 1

abs(leftmin[1] - rightmax[2]) = abs(-5 - 7) = 12

So the best answer for this partition is: 12. Checking all partition points gives the maximum absolute difference as: 12


Output
12
Comment