You are given an array W[1], W[2], …, W[N]. Choose K numbers among them such that the absolute difference between the sum of chosen numbers and the sum of remaining numbers is as large as possible.
Examples :
Input : arr[] = [8, 4, 5, 2, 10]
k = 2
Output: 17
Input : arr[] = [1, 1, 1, 1, 1, 1, 1, 1]
k = 3
Output: 2
There are two possibilities to get the desired answer. These two are:Choose k largest numbers or Choose k smallest numbers. Choose the best-suited option which fits according to the given values. This is because there are some cases in which the sum of smallest k numbers can be greater than rest of the array and there are some cases in which the sum of largest k numbers can be greater than rest of the sum of the numbers.
Approach :
- Sort the given array.
- Get the sum of all the numbers of the array and store it in sum
- Get the sum of first k numbers of the array and store it in sum1
- Get the sum of last k numbers of the array and store it in sum2
- Output the result which is : max(abs(S1-(S-S1)), abs(S2-(S-S2)))
C++
// C++ Program to find maximum weight // difference #include <iostream> #include <algorithm> using namespace std; // return the max value of two numbers int max(int a, int b) { return a > b ? a : b; } int solve(int array[], int n, int k) { // sort the given array sort(array, array + n); // Initializing the value to 0 int sum = 0, sum1 = 0, sum2 = 0; // Getting the sum of the array for (int i = 0; i < n; i++) { sum += array[i]; } // Getting the sum of first k elements for (int i = 0; i < k; i++) { sum1 += array[i]; } // Getting the sum of (n-k) elements for (int i = k; i < n; i++) { sum2 += array[i]; } // Returning the maximum possible difference. return max(abs(sum1 - (sum - sum1)), abs(sum2 - (sum - sum2))); } // Driver function int main() { int k = 2; int array[] = { 8, 4, 5, 2, 10 }; // calculate the numbers of elements in the array int n = sizeof(array) / sizeof(array[0]); // call the solve function cout << solve(array, n, k); return 0; } |
Java
// JAVA Code for Maximum Weight Difference import java.util.*; class GFG { public static int solve(int array[], int n, int k) { // sort the given array Arrays.sort(array); // Initializing the value to 0 int sum = 0, sum1 = 0, sum2 = 0; // Getting the sum of the array for (int i = 0; i < n; i++) { sum += array[i]; } // Getting the sum of first k elements for (int i = 0; i < k; i++) { sum1 += array[i]; } // Getting the sum of (n-k) elements for (int i = k; i < n; i++) { sum2 += array[i]; } // Returning the maximum possible difference. return Math.max(Math.abs(sum1 - (sum - sum1)), Math.abs(sum2 - (sum - sum2))); } /* Driver program to test above function */ public static void main(String[] args) { int k = 2; int array[] = { 8, 4, 5, 2, 10 }; // calculate the numbers of elements // in the array int n = array.length; // call the solve function System.out.print(solve(array, n, k)); } } // This code is contributed by Arnav Kr. Mandal. |
Python
def solve(array, k): # Sorting array array.sort() # Getting the sum of all the elements sum = sum(array) # Getting the sum of first k elements sum1 = sum(array[:k]) # Getting the sum last (n-k) elements sum2 = sum(array[k:]) # Returning the maximum possible difference return max(abs(s1-(s-s1)), abs(s2-(s-s2))) # Driver function k = 2array =[8, 4, 5, 2, 10] print(solve(array, k)) |
C#
// C# Code for Maximum Weight Difference using System; class GFG { public static int solve(int []array, int n, int k) { // sort the given array Array.Sort(array); // Initializing the value to 0 int sum = 0, sum1 = 0, sum2 = 0; // Getting the sum of the array for (int i = 0; i < n; i++) { sum += array[i]; } // Getting the sum of first k elements for (int i = 0; i < k; i++) { sum1 += array[i]; } // Getting the sum of (n-k) elements for (int i = k; i < n; i++) { sum2 += array[i]; } // Returning the maximum possible difference. return Math.Max(Math.Abs(sum1 - (sum - sum1)), Math.Abs(sum2 - (sum - sum2))); } /* Driver program to test above function */ public static void Main() { int k = 2; int []array = { 8, 4, 5, 2, 10 }; // calculate the numbers of elements // in the array int n = array.Length; // call the solve function Console.WriteLine(solve(array, n, k)); } } // This code is contributed by vt_m. |
PHP
<?php // PHP Program to find maximum weight // difference // return the max value of two numbers function maxi($a, $b) { if ($a > $b) { return $a; } else { return $b; } } function solve(&$arr, $n, $k) { // sort the given array sort($arr); // Initializing the value to 0 $sum = 0; $sum1 = 0; $sum2 = 0; // Getting the sum of the array for ($i = 0; $i < $n; $i++) { $sum += $arr[$i]; } // Getting the sum of first k elements for ($i = 0; $i < $k; $i++) { $sum1 += $arr[$i]; } // Getting the sum of (n-k) elements for ($i = $k; $i < $n; $i++) { $sum2 += $arr[$i]; } // Returning the maximum possible difference. return maxi(abs($sum1 - ($sum - $sum1)), abs($sum2 - ($sum - $sum2))); } // DriverCode $k = 2; $arr = array(8, 4, 5, 2, 10 ); // calculate the numbers of // elements in the array $n = sizeof($arr); // call the solve function echo (solve($arr, $n, $k)); // This code is contributed // by Shivi_Aggarwal ?> |
Output:
17
This article is contributed by Rishabh Bansal. 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 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.
Recommended Posts:
- Maximum XOR value of maximum and second maximum element among all possible subarrays
- Maximum element in an array such that its previous and next element product is maximum
- Find all unique pairs of maximum and second maximum elements over all sub-arrays in O(NlogN)
- Maximum of XOR of first and second maximum of all subarrays
- Maximum length Subsequence with alternating sign and maximum Sum
- Maximum increase in value of Matrix to keep maximum rows and columns unchanged
- Sliding Window Maximum (Maximum of all subarrays of size k) using stack in O(n) time
- Sliding Window Maximum (Maximum of all subarrays of size k)
- Maximum difference between two elements such that larger element appears after the smaller number
- Minimize the maximum difference between the heights
- Maximum absolute difference between sum of two contiguous sub-arrays
- Maximum sum of pairs with specific difference
- Find maximum difference between nearest left and right smaller elements
- Choose k array elements such that difference of maximum and minimum is minimized
- Maximum difference between first and last indexes of an element in array
- Find the sum of maximum difference possible from all subset of a given array.
- Maximum difference between group of k-elements and rest of the array.
- Count ways of choosing a pair with maximum difference
- k size subsets with maximum difference d between max and min
- Partition into two subarrays of lengths k and (N - k) such that the difference of sums is maximum

