Given an array of integers, find two non-overlapping contiguous sub-arrays such that the absolute difference between the sum of two sub-arrays is maximum.
Input: [-2, -3, 4, -1, -2, 1, 5, -3] Output: 12 Two subarrays are [-2, -3] and [4, -1, -2, 1, 5] Input: [2, -1, -2, 1, -4, 2, 8] Output: 16 Two subarrays are [-1, -2, 1, -4] and [2, 8]
Expected time complexity is O(n).
The idea is for each index i in given array arr[0…n-1], compute maximum and minimum sum subarrays that lie in subarrays arr[0…i] and arr[i+1 …n-1]. We maintain four arrays that store the maximum and minimum sums in the subarrays arr[0…i] and arr[i+1 … n-1] for every index i in the array.
leftMax : An element leftMax[i] of this array stores the maximum value in subarray arr[0..i] leftMin : An element leftMin[i] of this array stores the minimum value in subarray arr[0..i] rightMax : An element rightMax[i] of this array stores the maximum value in subarray arr[i+1..n-1] rightMin : An element rightMin[i] of this array stores the minimum value in subarray arr[i+1..n-1]
We can build above four arrays in O(n) time by using Kadane Algorithm.
- Kadane’s algorithm can be modified to find minimum absolute sum of a subarray as well. The idea is to change the sign of each element in the array and run Kadane Algorithm to find maximum sum subarray that lies in arr[0…i] and arr[i+1 … n-1]. Now invert the sign of maximum subarray sum found. That will be our minimum subarray sum. This idea is taken from here.
- abs(max sum subarray that lies in arr[0…i] – min sum subarray that lies in arr[i+1…n-1])
- abs(min sum subarray that lies in arr[0…i] – max sum subarray that lies in arr[i+1…n-1])
- Maximum product of sum of two contiguous subarrays of an array
- Maximum absolute difference between sum of subarrays of size K
- Queries to find maximum sum contiguous subarrays of given length in a rotating array
- Partitioning into two contiguous element subarrays with equal sums
- Minimum absolute difference of XOR values of two subarrays
- Count of ways to split an Array into three contiguous Subarrays having increasing Sum
- Maximum distance between two elements whose absolute difference is K
- Count of contiguous subarrays possible for every index by including the element at that index
- Maximize cost to empty an array by removing contiguous subarrays of equal elements
- Count of subarrays of size K having at least one pair with absolute difference divisible by K-1
- Number of subarrays having absolute sum greater than K | Set-2
- Maximise sum of absolute difference between adjacent elements in Array with sum K
- Differences between number of increasing subarrays and decreasing subarrays in k sized windows
- Maximum sum contiguous nodes in the given linked list
- Maximum XOR value of maximum and second maximum element among all possible subarrays
- Maximum absolute difference between distinct elements in an Array
- Remove Minimum coins such that absolute difference between any two piles is less than K
- Partition the array into two odd length groups with minimized absolute difference between their median
- Longest subarray in which absolute difference between any two element is not greater than X
- Count of numbers upto N having absolute difference of at most K between any two adjacent digits
In order to calculate maximum sum subarray that lies in arr[0…i], we run Kadane Algorithm from 0 to n-1 and to find maximum sum subarray that lies in arr[i+1 … n-1], we run Kadane Algorithm from n-1 to 0.
Now from above four arrays, we can easily find maximum absolute difference between the sum of two contiguous sub-arrays. For each index i, take maximum of
Below is the implementation of above idea.
Time Complexity is O(n) where n is the number of elements in input array. Auxiliary Space required is O(n).
This article is contributed by Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Improved By : chitranayal