Given an array of non-negative integers of length N and an integer K. Partition the given array into two subsets of length K and N – K so that the difference between the sum of both subsets is maximum.
Examples :
Input : arr[] = {8, 4, 5, 2, 10}
k = 2
Output : 17
Explanation :
Here, we can make first subset of length k = {4, 2}
and second subset 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, subsets would be {1, 1, 1, 1, 1} and {1, 1, 1}.
So, max_difference would be 2
Choose k numbers with the 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 subsets of k and N – k numbers respectively. Consider two cases:
- The subset with the largest sum, among these two subsets, is a subset of K numbers. Then we want to maximize the sum in it since the sum in the second subset will only decrease if the sum in the first subarray will increase. So we are now in the sub-problem considered above and should choose k largest numbers.
- The subset with the largest sum, among these two subsets, is a subset of N – k numbers. Similar to the previous case, we then have to choose N – k largest numbers among all numbers.
Now, Let’s think about which of the two above cases actually gives the answer. We can easily see that a larger difference would be when more numbers are included in 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++
#include <bits/stdc++.h>
using namespace std;
int maxDifference( int arr[], int N, int k)
{
int M, S = 0, S1 = 0, max_difference = 0;
for ( int i = 0; i < N; i++)
S += arr[i];
sort(arr, arr + N, greater< int >());
M = max(k, N - k);
for ( int i = 0; i < M; i++)
S1 += arr[i];
max_difference = S1 - (S - S1);
return max_difference;
}
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;
}
|
Java
import java.util.*;
class GFG
{
static int maxDifference( int arr[], int N, int k)
{
int M, S = 0 , S1 = 0 , max_difference = 0 ;
for ( int i = 0 ; i < N; i++)
S += arr[i];
int temp;
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];
max_difference = S1 - (S - S1);
return max_difference;
}
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));
}
}
|
Python3
def maxDifference(arr, N, k ):
S = 0
S1 = 0
max_difference = 0
for i in range (N):
S + = arr[i]
arr.sort(reverse = True )
M = max (k, N - k)
for i in range ( M):
S1 + = arr[i]
max_difference = S1 - (S - S1)
return max_difference
arr = [ 8 , 4 , 5 , 2 , 10 ]
N = len (arr)
k = 2
print (maxDifference(arr, N, k))
|
C#
using System;
class GFG
{
static int maxDifference( int []arr, int N, int k)
{
int M, S = 0, S1 = 0, max_difference = 0;
for ( int i = 0; i < N; i++)
S += arr[i];
int temp;
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];
max_difference = S1 - (S - S1);
return max_difference;
}
public static void Main()
{
int []arr = { 8, 4, 5, 2, 10 };
int N = arr.Length;
int k = 2;
Console.Write(maxDifference(arr, N, k));
}
}
|
PHP
<?php
function maxDifference( $arr , $N , $k )
{
$M ; $S = 0; $S1 = 0;
$max_difference = 0;
for ( $i = 0; $i < $N ; $i ++)
$S += $arr [ $i ];
rsort( $arr );
$M = max( $k , $N - $k );
for ( $i = 0; $i < $M ; $i ++)
$S1 += $arr [ $i ];
$max_difference = $S1 - ( $S - $S1 );
return $max_difference ;
}
$arr = array (8, 4, 5, 2, 10);
$N = count ( $arr );
$k = 2;
echo maxDifference( $arr , $N , $k );
?>
|
Javascript
<script>
function maxDifference(arr, N, k)
{
let M, S = 0, S1 = 0, max_difference = 0;
for (let i = 0; i < N; i++)
S += arr[i];
let temp;
for (let i = 0; i < N; i++)
{
for (let 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 (let i = 0; i < M; i++)
S1 += arr[i];
max_difference = S1 - (S - S1);
return max_difference;
}
let arr = [ 8, 4, 5, 2, 10 ];
let N = arr.length;
let k = 2;
document.write(maxDifference(arr, N, k));
</script>
|
Time Complexity: O(N log N), to sort the array
Auxiliary Space: O(1), as no extra space is used
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.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
27 Jul, 2022
Like Article
Save Article