# Maximum absolute difference between sum of subarrays of size K

• Difficulty Level : Easy
• Last Updated : 24 Mar, 2021

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:
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

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.
2.
• 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.
3. Calculate the sum of first K size subarray and update maxSum and minSum, decrement sum by arr[start] and  increment start by 1.
4. 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.
5. 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[0]);``    ` `    ``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`

## Javascript

 ``

Output:

`6`

Time Complexity: O (N)
Auxiliary Space: O (1)

My Personal Notes arrow_drop_up