Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*The brother-sister duo went to shopping and bought “N” items. The items are numbered
- from 1 to N, and the item “i” weighs “Wi” 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.
- Input:
- The first line of input contains an integer T, denoting the number of test cases.
- The first line of each test case contains two space-separated integers N and X.
- The next line contains N space-separated integers W1, W2… WN*/
- /*Input
- 2
- 5 2
- 8 4 5 2 10
- 8 3
- 1 1 1 1 1 1 1 1
- output
- 17
- 2*/
- /*aLSO available on Pastebin.com*/
- /*aLSO available on Pastebin.com*/
- #include<stdio.h>
- #include<stdlib.h>
- void sortarray(int item[], int n)
- {
- int i,j,temp;
- for(i=0;i<n-1;i++)
- {
- for(j=i+1;j<n;j++)
- {
- if(item[i]<item[j])
- {
- temp = item[i];
- item[i] = item[j];
- item[j]= temp;
- }
- }
- }
- }
- int main()
- { int test_case,i,n,x,item[30];
- int large,sum1,sum2;
- printf("Enter number of test cases: \n");
- scanf("%d",&test_case);
- while(test_case)
- {
- test_case--;
- printf("\nEnter the value of 'N' & 'X' according the question: ");
- scanf("%d %d",&n,&x);
- printf("\nEnter the weight of all %d items: \n",n);
- for(i=0;i<n;i++)
- {
- printf("Enter weight of item no %d: ",i+1);
- scanf("%d",&item[i]);
- }
- sortarray(item , n);//sorting array in discending order
- large = n-x >= x?n-x :x; //finding the number of items in larger group which brother have to carry
- sum1 = 0; sum2 = 0;
- for(i=0;i<n;i++)
- { if(i<large)
- sum1 = sum1 + item[i];//total weight sum of larger group
- else
- sum2 = sum2 + item[i];//total weight sum of smaller group
- }
- printf("\nMaximum differnde in weight = %d \n",sum1 - sum2);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement