Maximum weight Difference:
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 W; 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:
The first line of input contains two space-separated integers N and K.
The next line contains N space-separated integers W₁, W2, WN.
Output:
Output the maximum possible difference between the weights carried by both in grams.
Constraints:
• 1≤K<N≤ 100
• 1≤ W; ≤ 100000 (105)
Sample 1:
Input:
5 2
8 4 5 2 10
Output:
17
Explanation:
The optimal way is that Chef gives his son K-2 items with weights 2 and 4. Chef carries the rest of the items himself. Thus the difference is: (8+5+10) ? (4+2) = 23 ? 6 = 17.
Sample input E
5 2
8 4 5 2 10
Sample output
17
Program:
#c
#include<stdio.h>
#include<stdlib.h>
int compare(const void * a, const void * b){
return( *(int*)a - *(int*)b);
}
int solve(int a[], int n,int k){
int i,sum1=0 ,sum2=0;
qsort(a,n,sizeof(int),compare);
if(k <= n-k){
for(i=k ;i<n; i++)
sum1+=a[i];
for(i=0; i<k; i++)
sum2+=a[i];
return sum1 - sum2;
}
else{
for(i=0 ;i<n; i++)
sum2+=a[i];
for(i=n-k; i<n; i++)
sum1+=a[i];
return sum1 - sum2;
}
}
int main(){
int n,k,i,ans;
scanf("%d %d",&n,&k);
int a[n];
for(i=0; i<n; i++){
scanf("%d",&a[i]);
}
ans=solve(a,n,k);
printf("%d\n",ans);
return 0;
}
#Python
n,k=map(int,input().split())
weights = list(map(int,input().split()))
weights.sort()
s_weights = weights[:k]
c_weights = weights[k:]
s_w_total = sum(s_weights)
c_w_total = sum(c_weights)
max_diff = c_w_total - s_w_total
printf(max_diff)
Explanation:
Code that takes two inputs, n and k, followed by a list of n weights. The weights are sorted in ascending order, and then split into two lists: s_weights containing the k smallest weights, and c_weights containing the remaining weights.
The code then calculates the total weight of both lists using the sum function, and computes the difference between the total weight of c_weights and s_weights. This difference is stored in the variable max_diff.
Finally, the code attempts to print the value of max_diff using a function called printf. However, this function does not exist in Python - the correct function to use would be print(max_diff).
In summary, this code sorts the input weights, splits them into two lists, and calculates the maximum difference between the total weight of the larger list and the total weight of the k smallest weights.
0 Comments