Given an array of non-negative integers of length N and an integer k. Partition the given array into two subarrays of length K and N – k so that the difference between the sum of both subarray is maximum.

**Examples :**

Input : arr[] = {8, 4, 5, 2, 10} k = 2 Output : 17 Explanation : Here, we can make first subarray of length k = {4, 2} and second subarray of length N - k = {8, 5, 10}. Then, the max_difference = (8 + 5 + 10) - (4 + 2) = 17. Input : arr[] = {1, 1, 1, 1, 1, 1, 1, 1} k = 3 Output : 2 Explanation : Here, subarrays would be {1, 1, 1, 1, 1} and {1, 1, 1}. So, max_difference would be 2.

Choose k numbers with largest possible sum. Then the solution obviously is k largest numbers. So that here greedy algorithm works – at each step we choose the largest possible number until we get all K numbers.

In this problem we should divide the array of N numbers into two subarrays of* k* and *N – k* numbers respectively. Consider two cases –

- The subarray with largest sum, among these two subarrays, is subarray of K numbers. Then we want to maximize the sum in it, since the sum in the second subarray will only decrease if the sum in the first subarray will increase. So we are now in sub-problem considered above and should choose k largest numbers.
- The subarray with largest sum, among these two subarray, is subarray of N – k numbers. Similarly to the previous case we then have to choose N – k largest numbers among all numbers.

Now, Let’s think which of the two above cases actually gives the answer. We can easily see that larger difference would be when more numbers are included to the group of largest numbers. Hence we could set M = max(k, N – k), find the sum of M largest numbers (let it be S1) and then the answer is S1 – (S – S1), where S is the sum of all numbers.

Below is the implementation of the above approach :

## C++

`// CPP program to calculate max_difference between ` `// the sum of two subarrays of length k and N - k ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to calculate max_difference ` `int` `maxDifference(` `int` `arr[], ` `int` `N, ` `int` `k) ` `{ ` ` ` `int` `M, S = 0, S1 = 0, max_difference = 0; ` ` ` ` ` `// Sum of the array ` ` ` `for` `(` `int` `i = 0; i < N; i++) ` ` ` `S += arr[i]; ` ` ` ` ` `// Sort the array in descending order ` ` ` `sort(arr, arr + N, greater<` `int` `>()); ` ` ` `M = max(k, N - k); ` ` ` `for` `(` `int` `i = 0; i < M; i++) ` ` ` `S1 += arr[i]; ` ` ` ` ` `// Calculating max_difference ` ` ` `max_difference = S1 - (S - S1); ` ` ` `return` `max_difference; ` `} ` ` ` `// Driver function ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 8, 4, 5, 2, 10 }; ` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `int` `k = 2; ` ` ` `cout << maxDifference(arr, N, k) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to calculate max_difference between ` `// the sum of two subarrays of length k and N - k ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to calculate max_difference ` `static` `int` `maxDifference(` `int` `arr[], ` `int` `N, ` `int` `k) ` `{ ` ` ` `int` `M, S = ` `0` `, S1 = ` `0` `, max_difference = ` `0` `; ` ` ` ` ` `// Sum of the array ` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++) ` ` ` `S += arr[i]; ` ` ` `int` `temp; ` ` ` ` ` `// Sort the array in descending order ` ` ` `for` `(` `int` `i = ` `0` `; i < N; i++) ` ` ` `{ ` ` ` `for` `(` `int` `j = i + ` `1` `; j < N; j++) ` ` ` `{ ` ` ` `if` `(arr[i] < arr[j]) ` ` ` `{ ` ` ` `temp = arr[i]; ` ` ` `arr[i] = arr[j]; ` ` ` `arr[j] = temp; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `M = Math.max(k, N - k); ` ` ` `for` `(` `int` `i = ` `0` `; i < M; i++) ` ` ` `S1 += arr[i]; ` ` ` ` ` `// Calculating max_difference ` ` ` `max_difference = S1 - (S - S1); ` ` ` `return` `max_difference; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `arr[] = { ` `8` `, ` `4` `, ` `5` `, ` `2` `, ` `10` `}; ` ` ` `int` `N = arr.length; ` ` ` `int` `k = ` `2` `; ` ` ` `System.out.println(maxDifference(arr, N, k)); ` `} ` `} ` ` ` `// This code is contributed by ` `// Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 code to calculate max_difference ` `# between the sum of two subarrays of ` `# length k and N - k ` ` ` `# Function to calculate max_difference ` `def` `maxDifference(arr, N, k ): ` ` ` `S ` `=` `0` ` ` `S1 ` `=` `0` ` ` `max_difference ` `=` `0` ` ` ` ` `# Sum of the array ` ` ` `for` `i ` `in` `range` `(N): ` ` ` `S ` `+` `=` `arr[i] ` ` ` ` ` `# Sort the array in descending order ` ` ` `arr.sort(reverse` `=` `True` `) ` ` ` `M ` `=` `max` `(k, N ` `-` `k) ` ` ` `for` `i ` `in` `range` `( M): ` ` ` `S1 ` `+` `=` `arr[i] ` ` ` ` ` `# Calculating max_difference ` ` ` `max_difference ` `=` `S1 ` `-` `(S ` `-` `S1) ` ` ` `return` `max_difference ` ` ` `# Driver Code ` `arr ` `=` `[ ` `8` `, ` `4` `, ` `5` `, ` `2` `, ` `10` `] ` `N ` `=` `len` `(arr) ` `k ` `=` `2` `print` `(maxDifference(arr, N, k)) ` ` ` `# This code is contributed by "Sharad_Bhardwaj". ` |

*chevron_right*

*filter_none*

## C#

`// C# program to calculate max_difference between ` `// the sum of two subarrays of length k and N - k ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to calculate max_difference ` `static` `int` `maxDifference(` `int` `[]arr, ` `int` `N, ` `int` `k) ` `{ ` ` ` `int` `M, S = 0, S1 = 0, max_difference = 0; ` ` ` ` ` `// Sum of the array ` ` ` `for` `(` `int` `i = 0; i < N; i++) ` ` ` `S += arr[i]; ` ` ` `int` `temp; ` ` ` ` ` `// Sort the array in descending order ` ` ` `for` `(` `int` `i = 0; i < N; i++) ` ` ` `{ ` ` ` `for` `(` `int` `j = i + 1; j < N; j++) ` ` ` `{ ` ` ` `if` `(arr[i] < arr[j]) ` ` ` `{ ` ` ` `temp = arr[i]; ` ` ` `arr[i] = arr[j]; ` ` ` `arr[j] = temp; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `M = Math.Max(k, N - k); ` ` ` `for` `(` `int` `i = 0; i < M; i++) ` ` ` `S1 += arr[i]; ` ` ` ` ` `// Calculating max_difference ` ` ` `max_difference = S1 - (S - S1); ` ` ` `return` `max_difference; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `[]arr = { 8, 4, 5, 2, 10 }; ` ` ` `int` `N = arr.Length; ` ` ` `int` `k = 2; ` ` ` `Console.Write(maxDifference(arr, N, k)); ` `} ` `} ` ` ` `// This code is contributed by mohit kumar 29 ` ` ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to calculate ` `// max_difference between ` `// the sum of two subarrays ` `// of length k and N - k ` ` ` `// Function to calculate ` `// max_difference ` `function` `maxDifference(` `$arr` `, ` `$N` `, ` `$k` `) ` `{ ` ` ` `$M` `; ` `$S` `= 0; ` `$S1` `= 0; ` ` ` `$max_difference` `= 0; ` ` ` ` ` `// Sum of the array ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$N` `; ` `$i` `++) ` ` ` `$S` `+= ` `$arr` `[` `$i` `]; ` ` ` ` ` `// Sort the array in ` ` ` `// descending order ` ` ` `rsort(` `$arr` `); ` ` ` `$M` `= max(` `$k` `, ` `$N` `- ` `$k` `); ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$M` `; ` `$i` `++) ` ` ` `$S1` `+= ` `$arr` `[` `$i` `]; ` ` ` ` ` `// Calculating ` ` ` `// max_difference ` ` ` `$max_difference` `= ` `$S1` `- (` `$S` `- ` `$S1` `); ` ` ` `return` `$max_difference` `; ` `} ` ` ` `// Driver Code ` `$arr` `= ` `array` `(8, 4, 5, 2, 10); ` `$N` `= ` `count` `(` `$arr` `); ` `$k` `= 2; ` `echo` `maxDifference(` `$arr` `, ` `$N` `, ` `$k` `); ` ` ` `// This code is contributed ` `// by anuj_67. ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

17

**Further Optimizations : ** We can use Heap (or priority queue) to find M largest elements efficiently. Refer k largest(or smallest) elements in an array for details.

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.

## Recommended Posts:

- Partition a set into two non-empty subsets such that the difference of subset sums is maximum
- Partitioning into two contiguous element subarrays with equal sums
- Split array into two subarrays such that difference of their maximum is minimum
- Maximum sum of lengths of non-overlapping subarrays with k as the max element.
- Partition an array such into maximum increasing segments
- Partition first N natural number into two sets such that their sum is not coprime
- Split array into two subarrays such that difference of their sum is minimum
- Partition the array into two odd length groups with minimized absolute difference between their median
- Split a given array into K subarrays minimizing the difference between their maximum and minimum
- Number of ways to partition a string into two balanced subsequences
- Maximum XOR value of maximum and second maximum element among all possible subarrays
- Check if the product of the K largest sums of subarrays is greater than M
- Lengths of maximized partitions of a string such that each character of the string appears in one substring
- Check if it possible to partition in k subarrays with equal sum
- Split array into K disjoint subarrays such that sum of each subarray is odd.
- Split array to three subarrays such that sum of first and third subarray is equal and maximum
- Find the sums for which an array can be divided into sub-arrays of equal sum
- Find if array can be divided into two subarrays of equal sum
- Split an array into two equal Sum subarrays
- Number of times an array can be partitioned repetitively into two subarrays with equal sum

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.