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])
- Minimum absolute difference of XOR values of two subarrays
- Maximum absolute difference in an array
- Maximum sum of absolute difference of an array
- Maximum absolute difference of value and index sums
- Partition into two subarrays of lengths k and (N - k) such that the difference of sums is maximum
- Count maximum elements of an array whose absolute difference does not exceed K
- Find maximum number of elements such that their absolute difference is less than or equal to 1
- Partitioning into two contiguous element subarrays with equal sums
- Number of subarrays having absolute sum greater than K | Set-2
- Sliding Window Maximum (Maximum of all subarrays of size k) using stack in O(n) time
- Sliding Window Maximum (Maximum of all subarrays of size k)
- K maximum sums of non-overlapping contiguous sub-arrays
- Maximum sum contiguous nodes in the given linked list
- K maximum sums of overlapping contiguous sub-arrays
- Find the count of maximum contiguous Even numbers
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 email@example.com. 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
Improved By : chitranayal