- 1

Help me this in c language

8. The brother-sister duo went to shopping and bought "N" items. The items are numbered from 1 to N, and the item "i" weighs "W" grams. Little sister insists on helping his brother in carrying the items. She wants her brother to give her a few items. But Big Brother does not want to burden his sister. But she won't stop bothering him unless she is given a few items to carry. So Brother decides to give her some items. Obviously, Brother wants to give his sister less weight to carry. However, his sister is a smart kid. To avoid being given the bare minimum weight to carry, she suggests that the items are split into two groups, and one group contains exactly "X" items. Then Brother will carry the heavier group, and his sister will carry the other group. Help the Brother in deciding which items his sister should take. Tell the Brother the maximum possible difference between the weight carried by him and the weight carried by his sister.

26th Oct 2022, 1:20 PM
Amishree Turakhia
Amishree Turakhia - avatar
1 Answer
+ 1
If I understand correctly, one solution would be to sort the weights, add up the lightest X weights for Little sister's load. Add up the remaining weights for Brother's load. Find the difference. The result will be the maximum difference between the two loads. //some psuedocode //Input weights W[0 ... N-1] ... //sort array W in ascending order ... //Maximize the difference... //Add up L in a loop L = 0 L += W[0] +W[1] ...+W[X-1] //Add up B in a loop B = 0 B += W[X] +W[X+1] ...+W[N-1] //output maxDiff maxDiff = B - L ...
26th Oct 2022, 11:31 PM
Brian
Brian - avatar