# Maximum absolute difference between sum of subarrays of size K

Given an array arr[] of size N and an integer K, the task is to find maximum absolute difference between the sum of subarrays of size K.

Examples :

Input: arr[] = {-2, -3, 4, -1, -2, 1, 5, -3}, K = 3
Output: 6
Explanation::
Sum of subarray (-2, -3, 4) = -1
Sum of subarray (-3, 4, -1) = 0
Sum of subarray (4, -1, -2) = 1
Sum of subarray (-1, -2, 1) = -2
Sum of subarray (-2, 1, 5) = 4
Sum of subarray (1, 5, -3) = 3
So maximum absolute difference between sum of subarray of size 3 is is (4 – (-2)) = 6.

Input: arr [ ] = {2, 5, -1, 7, -3, -1, -2}, K = 4
Output: 12

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Naive Approach
The simplest approach is to generate all subarrays of size K and find the minimum sum and maximum sum among them. Finally, return the absolute difference between the maximum and minimum sums.
Time Complexity: O( N2)
Auxiliary Space: O (1)

Efficient Approach
The idea is to use Sliding Window Technique. Follow the steps below to solve the problem:

1. Check if K is greater than N then return -1.
• Initialise following variables :

• maxSum : Store maximum sum of K size subarray.
• minSum : Store minimum sum of K size subarray.
• sum : Store current sum of K size subarray.
• start : Remove left most element which is no longer part of K size subarray.
2. Calculate the sum of first K size subarray and update maxSum and minSum, decrement sum by arr[start] and  increment start by 1.
3. Traverse arr from K to N and do the following operations:
• Increment sum by arr[i].
• Update maxSum and minSum.
• Decrement sum by arr[start].
• Increment start by 1.
4. Return absolute difference between maxSum and minSum.

Below is the implementation of the above approach :

## C++

 `// C++ program to find the  ` `// maximum absolute difference  ` `// between the sum of all  ` `// subarrays of size K  ` `#include ` `using` `namespace` `std; ` ` `  `// Return absolute difference  ` `// between sum of all subarrays  ` `// of size k  ` `int` `MaxAbsSumOfKsubArray(``int` `arr[],  ` `                         ``int` `K, ``int` `N)  ` `{  ` `     `  `    ``// Stores maximum sum of  ` `    ``// all K size subarrays  ` `    ``int` `maxSum = INT_MIN;  ` ` `  `    ``// Stores minimum sum of  ` `    ``// all K size subarray  ` `    ``int` `minSum = INT_MAX;  ` ` `  `    ``// Stores the sum of current  ` `    ``// subarray of size K  ` `    ``int` `sum = 0;  ` ` `  `    ``// Starting index of the  ` `    ``// current subarray  ` `    ``int` `start = 0;  ` ` `  `    ``int` `i = 0;  ` ` `  `    ``if` `(N < K)  ` `        ``return` `-1;  ` ` `  `    ``// Calculate the sum of  ` `    ``// first K elements  ` `    ``while` `(i < K) ` `    ``{  ` `        ``sum += arr[i];  ` `        ``i++;  ` `    ``}  ` ` `  `    ``// Update maxSum and minSum  ` `    ``maxSum = max(maxSum, sum);  ` `    ``minSum = min(minSum, sum);  ` ` `  `    ``// Decrement sum by arr[start]  ` `    ``// and increment start by 1  ` `    ``sum -= arr[start++];  ` ` `  `    ``// Traverse arr for the  ` `    ``// remaining subarrays  ` `    ``while` `(i < N)  ` `    ``{  ` ` `  `        ``// Increment sum by arr[i]  ` `        ``sum += arr[i];  ` ` `  `        ``// Increment i  ` `        ``i++;  ` ` `  `        ``// Update maxSum and minSum  ` `        ``maxSum = max(maxSum, sum);  ` `        ``minSum = min(minSum, sum);  ` ` `  `        ``// Decrement sum by arr[start]  ` `        ``// and increment start by 1  ` `        ``sum -= arr[start++];  ` `    ``}  ` ` `  `    ``// Return absolute difference  ` `    ``// between maxSum and minSum  ` `    ``return` `abs``(maxSum - minSum);  ` `}  ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `arr[] = { -2, -3, 4, -1,  ` `                  ``-2, 1, 5, -3 };  ` `    ``int` `K = 3;  ` `    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr);  ` `     `  `    ``cout << MaxAbsSumOfKsubArray(arr, K, N) ` `         ``<< endl; ` `          `  `    ``return` `0; ` `} ` ` `  `// This code is contributed by divyeshrabadiya07 `

## Java

 `// Java program to find the ` `// maximum absolute difference ` `// between the sum of all ` `// subarrays of size K ` ` `  `import` `java.util.*; ` ` `  `class` `GFG { ` ` `  `    ``// Return absolute difference ` `    ``// between sum of all subarrays ` `    ``// of size k ` `    ``static` `int` `MaxAbsSumOfKsubArray( ` `        ``int``[] arr, ` `        ``int` `K, ``int` `N) ` `    ``{ ` `        ``// Stores maximum sum of ` `        ``// all K size subarrays ` `        ``int` `maxSum = Integer.MIN_VALUE; ` ` `  `        ``// Stores minimum sum of ` `        ``// all K size subarray ` `        ``int` `minSum = Integer.MAX_VALUE; ` ` `  `        ``// Stores the sum of current ` `        ``// subarray of size K ` `        ``int` `sum = ``0``; ` ` `  `        ``// Starting index of the ` `        ``// current subarray ` `        ``int` `start = ``0``; ` ` `  `        ``int` `i = ``0``; ` ` `  `        ``if` `(N < K) ` `            ``return` `-``1``; ` ` `  `        ``// Calculate the sum of ` `        ``// first K elements ` `        ``while` `(i < K) { ` `            ``sum += arr[i]; ` `            ``i++; ` `        ``} ` ` `  `        ``// Update maxSum and minSum ` `        ``maxSum = Math.max(maxSum, sum); ` `        ``minSum = Math.min(minSum, sum); ` ` `  `        ``// Decrement sum by arr[start] ` `        ``// and increment start by 1 ` `        ``sum -= arr[start++]; ` ` `  `        ``// Traverse arr for the ` `        ``// remaining subarrays ` `        ``while` `(i < N) { ` ` `  `            ``// Increment sum by arr[i] ` `            ``sum += arr[i]; ` ` `  `            ``// Increment i ` `            ``i++; ` ` `  `            ``// Update maxSum and minSum ` `            ``maxSum = Math.max(maxSum, sum); ` `            ``minSum = Math.min(minSum, sum); ` ` `  `            ``// Decrement sum by arr[start] ` `            ``// and increment start by 1 ` `            ``sum -= arr[start++]; ` `        ``} ` ` `  `        ``// Return absolute difference ` `        ``// between maxSum and minSum ` `        ``return` `Math.abs(maxSum - minSum); ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args) ` `    ``{ ` `        ``int``[] arr = { -``2``, -``3``, ``4``, -``1``, ` `                      ``-``2``, ``1``, ``5``, -``3` `}; ` `        ``int` `K = ``3``; ` `        ``int` `N = arr.length; ` `        ``System.out.println( ` `            ``MaxAbsSumOfKsubArray( ` `                ``arr, K, N)); ` `    ``} ` `} `

## Python3

 `# Python3 program to find the  ` `# maximum absolute difference  ` `# between the sum of all  ` `# subarrays of size K  ` `import` `sys ` ` `  `# Return absolute difference  ` `# between sum of all subarrays  ` `# of size k  ` `def` `MaxAbsSumOfKsubArray(arr, K, N):  ` `     `  `    ``# Stores maximum sum of  ` `    ``# all K size subarrays  ` `    ``maxSum ``=` `-` `sys.maxsize ``-` `1` ` `  `    ``# Stores minimum sum of  ` `    ``# all K size subarray  ` `    ``minSum ``=` `sys.maxsize  ` ` `  `    ``# Stores the sum of current  ` `    ``# subarray of size K  ` `    ``sum` `=` `0` ` `  `    ``# Starting index of the  ` `    ``# current subarray  ` `    ``start ``=` `0` ` `  `    ``i ``=` `0` ` `  `    ``if` `(N < K): ` `        ``return` `-``1` ` `  `    ``# Calculate the sum of  ` `    ``# first K elements  ` `    ``while` `(i < K):  ` `        ``sum` `+``=` `arr[i]  ` `        ``i ``+``=` `1` `     `  `    ``# Update maxSum and minSum  ` `    ``maxSum ``=` `max``(maxSum, ``sum``)  ` `    ``minSum ``=` `min``(minSum, ``sum``)  ` ` `  `    ``# Decrement sum by arr[start]  ` `    ``# and increment start by 1  ` `    ``sum` `-``=` `arr[start]  ` `    ``start ``+``=` `1` ` `  `    ``# Traverse arr for the  ` `    ``# remaining subarrays  ` `    ``while` `(i < N):  ` ` `  `        ``# Increment sum by arr[i]  ` `        ``sum` `+``=` `arr[i]  ` ` `  `        ``# Increment i  ` `        ``i ``+``=` `1` ` `  `        ``# Update maxSum and minSum  ` `        ``maxSum ``=` `max``(maxSum, ``sum``)  ` `        ``minSum ``=` `min``(minSum, ``sum``)  ` ` `  `        ``# Decrement sum by arr[start]  ` `        ``# and increment start by 1  ` `        ``sum` `-``=` `arr[start]  ` `        ``start ``+``=` `1` ` `  `    ``# Return absolute difference  ` `    ``# between maxSum and minSum  ` `    ``return` `abs``(maxSum ``-` `minSum)  ` ` `  `# Driver code ` `arr ``=` `[ ``-``2``, ``-``3``, ``4``, ``-``1``,  ` `        ``-``2``, ``1``, ``5``, ``-``3` `]  ` `K ``=` `3` `N ``=` `len``(arr)  ` `     `  `print``(MaxAbsSumOfKsubArray(arr, K, N)) ` ` `  `# This code is contributed by sanjoy_62 `

## C#

 `// C# program to find the ` `// maximum absolute difference ` `// between the sum of all ` `// subarrays of size K ` `using` `System; ` `class` `GFG{ ` ` `  `// Return absolute difference ` `// between sum of all subarrays ` `// of size k ` `static` `int` `MaxAbsSumOfKsubArray( ` `       ``int``[] arr, ` `       ``int` `K, ``int` `N) ` `{ ` `    ``// Stores maximum sum of ` `    ``// all K size subarrays ` `    ``int` `MaxSum = Int32.MinValue; ` ` `  `    ``// Stores minimum sum of ` `    ``// all K size subarray ` `    ``int` `MinSum = Int32.MaxValue; ` ` `  `    ``// Stores the sum of current ` `    ``// subarray of size K ` `    ``int` `sum = 0; ` ` `  `    ``// Starting index of the ` `    ``// current subarray ` `    ``int` `start = 0; ` ` `  `    ``int` `i = 0; ` ` `  `    ``if` `(N < K) ` `        ``return` `-1; ` ` `  `    ``// Calculate the sum of ` `    ``// first K elements ` `    ``while` `(i < K) ` `    ``{ ` `        ``sum += arr[i]; ` `        ``i++; ` `    ``} ` ` `  `    ``// Update maxSum and minSum ` `    ``MaxSum = Math.Max(MaxSum, sum); ` `    ``MinSum = Math.Min(MinSum, sum); ` ` `  `    ``// Decrement sum by arr[start] ` `    ``// and increment start by 1 ` `    ``sum -= arr[start++]; ` ` `  `    ``// Traverse arr for the ` `    ``// remaining subarrays ` `    ``while` `(i < N)  ` `    ``{ ` ` `  `        ``// Increment sum by arr[i] ` `        ``sum += arr[i]; ` ` `  `        ``// Increment i ` `        ``i++; ` ` `  `        ``// Update maxSum and minSum ` `        ``MaxSum = Math.Max(MaxSum, sum); ` `        ``MinSum = Math.Min(MinSum, sum); ` ` `  `        ``// Decrement sum by arr[start] ` `        ``// and increment start by 1 ` `        ``sum -= arr[start++]; ` `    ``} ` ` `  `    ``// Return absolute difference ` `    ``// between maxSum and minSum ` `    ``return` `Math.Abs(MaxSum - MinSum); ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``int``[] arr = { -2, -3, 4, -1, ` `                  ``-2, 1, 5, -3 }; ` `    ``int` `K = 3; ` `    ``int` `N = arr.Length; ` `    ``Console.Write(MaxAbsSumOfKsubArray(arr, K, N)); ` `} ` `} ` ` `  `// This code is contributed  ` `// by shivanisinghss2110 `

Output:

```6
```

Time Complexity: O (N)
Auxiliary Space: O (1) My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.