Advertisement
Mr_kindle

brosis.c

Oct 26th, 2022 (edited)
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.93 KB | None | 0 0
  1. /*The brother-sister duo went to shopping and bought “N” items. The items are numbered
  2. from 1 to N, and the item “i” weighs “Wi” grams. Little sister insists on helping his brother in
  3. carrying the items. She wants her brother to give her a few items. But Big Brother does not
  4. want to burden his sister. But she won't stop bothering him unless she is given a few items to
  5. carry. So Brother decides to give her some items. Obviously, Brother wants to give his sister
  6. less weight to carry. However, his sister is a smart kid. To avoid being given the bare minimum
  7. weight to carry, she suggests that the items are split into two groups, and one group contains
  8. exactly “X” items. Then Brother will carry the heavier group, and his sister will carry the other
  9. group.
  10. Help the Brother in deciding which items his sister should take. Tell the Brother the maximum
  11. possible difference between the weight carried by him and the weight carried by his sister.
  12. Input:
  13.  The first line of input contains an integer T, denoting the number of test cases.
  14.  The first line of each test case contains two space-separated integers N and X.
  15.  The next line contains N space-separated integers W1, W2… WN*/
  16. /*Input
  17. 2
  18. 5 2
  19. 8 4 5 2 10
  20. 8 3
  21. 1 1 1 1 1 1 1 1
  22. output
  23. 17
  24. 2*/
  25.  
  26.  
  27. /*aLSO available on Pastebin.com*/
  28.  
  29. /*aLSO available on Pastebin.com*/
  30.  
  31. #include<stdio.h>
  32. #include<stdlib.h>
  33.  
  34. void sortarray(int item[], int n)
  35. {
  36.     int i,j,temp;
  37.     for(i=0;i<n-1;i++)
  38.         {
  39.             for(j=i+1;j<n;j++)
  40.                 {
  41.                     if(item[i]<item[j])
  42.                         {
  43.                             temp = item[i];
  44.                             item[i] = item[j];
  45.                             item[j]= temp;
  46.  
  47.                         }
  48.                 }
  49.         }
  50. }
  51.  
  52. int main()
  53. {   int test_case,i,n,x,item[30];
  54.     int large,sum1,sum2;
  55.     printf("Enter number of test cases:  \n");
  56.     scanf("%d",&test_case);
  57.     while(test_case)
  58.     {
  59.        
  60.  
  61.             test_case--;
  62.             printf("\nEnter the value of 'N' & 'X' according the question: ");
  63.             scanf("%d %d",&n,&x);
  64.             printf("\nEnter the weight of all %d items: \n",n);
  65.             for(i=0;i<n;i++)
  66.                 {
  67.                     printf("Enter weight of item no %d: ",i+1);
  68.                     scanf("%d",&item[i]);
  69.                 }
  70.             sortarray(item , n);//sorting array in discending order
  71.             large = n-x >= x?n-x :x; //finding the number of items in larger group which brother have to carry
  72.             sum1 = 0; sum2 = 0;
  73.             for(i=0;i<n;i++)
  74.                 {   if(i<large)
  75.                         sum1 = sum1 + item[i];//total weight sum of larger group
  76.                     else
  77.                         sum2 = sum2 + item[i];//total weight sum of smaller group
  78.                 }
  79.             printf("\nMaximum differnde in weight = %d \n",sum1 - sum2);
  80.            
  81.     }
  82.     return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement