Hello coders, today we are going to solve Maximum Weight Difference CodeChef Solution whose Problem Code is MAXDIFF.
Task
Chef has gone shopping with his 5-year old son. They have bought N items so far. The items are numbered from 1 to N, and the item i weighs Wi grams.
Chef’s son insists on helping his father in carrying the items. He wants his dad to give him a few items. Chef does not want to burden his son. But he won’t stop bothering him unless he is given a few items to carry. So Chef decides to give him some items. Obviously, Chef wants to give the kid less weight to carry.
However, his son is a smart kid. To avoid being given the bare minimum weight to carry, he suggests that the items are split into two groups, and one group contains exactly K items. Then Chef will carry the heavier group, and his son will carry the other group.
Help the Chef in deciding which items should the son take. Your task will be simple. Tell the Chef the maximum possible difference between the weight carried by him and the weight carried by the kid.
Input Format
The first line of input contains an integer T, denoting the number of test cases. Then T test cases follow. The first line of each test contains two space-separated integers N and K. The next line contains N space-separated integers W1, W2, …, WN.
Output Format
For each test case, output the maximum possible difference between the weights carried by both in grams.
Constraints
- 1 ≤ T ≤ 100
- 1 ≤ K < N ≤ 100
- 1 ≤ Wi ≤ 100000 (105)
Example
Sample Input
2
5 2
8 4 5 2 10
8 3
1 1 1 1 1 1 1 1
Sample Output
17
2
Solution – Maximum Weight Difference
C++
#include <bits/stdc++.h> using namespace std; int solve(int a[], int n, int k) { sort(a, a + n); if (k <= n - k) { return accumulate(a + k, a + n, 0) - accumulate(a, a + k, 0); } return accumulate(a + n - k, a + n, 0) - accumulate(a, a + n - k, 0); } int main() { int t; cin >> t; while (t--) { int n, k; cin >> n >> k; int a[n]; for (int i = 0; i < n; i++) { cin >> a[i]; } int ans = solve(a, n, k); cout << ans << endl; } return 0; }
Python
# cook your dish here for _ in range(int(input())): N,K=map(int,input().split()) w=list(map(int,input().split())) w.sort() son=sum(w[:K]) dad=sum(w[K:]) first=dad-son w.reverse() min=sum(w[:K]) max=sum(w[K:]) sec=min-max if first<sec: print(sec) else: print(first)
Java
/* package codechef; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ class Codechef { public static void main (String[] args) throws java.lang.Exception { // your code goes here Scanner scan= new Scanner(System.in); int T,K,N; T=scan.nextInt(); while(T>0) { N=scan.nextInt(); K=scan.nextInt(); int W[]=new int[N]; for(int i=0;i<N;i++) { W[i]=scan.nextInt(); } Arrays.sort(W); int sum_son=0,sum_chef=0; if(K<=N-K) { for(int i=0;i<K;i++) { sum_son=sum_son+W[i]; } for(int i=K;i<N;i++) { sum_chef=sum_chef+W[i]; } } else { for(int i=0;i<N-K;i++) { sum_son=sum_son+W[i]; } for(int i=N-K;i<N;i++) { sum_chef=sum_chef+W[i]; } } System.out.println(sum_chef-sum_son); T--; } } }
Disclaimer: The above Problem (Maximum Weight Difference) is generated by CodeChef but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.