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 absolute difference between sum of subarrays of size K
- Maximum product of sum of two contiguous subarrays of an array
- Minimum absolute difference of XOR values of two subarrays
- Count of subarrays of size K having at least one pair with absolute difference divisible by K-1
- Maximum sum of absolute difference of any permutation
- Maximum absolute difference of value and index sums
- Maximum absolute difference between distinct elements in an Array
- Count maximum elements of an array whose absolute difference does not exceed K
- Minimum value of maximum absolute difference of all adjacent pairs in an Array
- Find maximum number of elements such that their absolute difference is less than or equal to 1
- Find K elements whose absolute difference with median of array is maximum
- Subsequence with maximum pairwise absolute difference and minimum size
- Partition into two subarrays of lengths k and (N - k) such that the difference of sums is maximum
- Split array into two subarrays such that difference of their maximum is minimum
- Min difference between maximum and minimum element in all Y size subarrays
- Minimize the maximum absolute difference of adjacent elements in a circular array
- Split a given array into K subarrays minimizing the difference between their maximum and minimum
- Partitioning into two contiguous element subarrays with equal sums
- Missing occurrences of a number in an array such that maximum absolute difference of adjacent elements is minimum
- Number of subarrays having absolute sum greater than K | Set-2
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