Advertisement
Guest User

Untitled

a guest
Mar 24th, 2022
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. ## Problem Overview
  2.  
  3. Bill Gates is on one of his philanthropic journeys to a village in Utopia. He has N packets of candies and would like to distribute one packet to each of the K children in the village (each packet may contain different number of candies). To avoid any fighting among the children, he would like to pick K out of N packets, such that unfairness is minimized.
  4.  
  5. Suppose the K packets have (x1, x2, x3,....xk) candies in them, where xi denotes the number of candies in the ith packet, then we define unfairness as
  6.  
  7. max(x1,x2,...xk) - min(x1,x2,...xk)
  8.  
  9. where max denotes the highest value amongst the elements, and min denotes the least value amongst the elements. Can you figure out the minimum unfairness and print it?
  10.  
  11. Input
  12. The first line contains an integer N.
  13. The second line contains an integer K. N lines follow. Each line contains an integer that denotes the candy in the ith packet.
  14.  
  15. Output
  16. An integer that denotes the minimum possible value of unfairness.
  17.  
  18.  
  19. ## Program that works for all test cases
  20.  
  21. (Compiled & Tested on Linux with GNU GCC)
  22.  
  23. {% highlight c %}
  24. #include<stdio.h>
  25. #include<math.h>
  26. #include<stdlib.h>
  27.  
  28. /*
  29. * Comparison function required by qsort -
  30. * The comparison function must return an integer less
  31. * than, equal to, or greater than zero if the first
  32. * argument is considered to be respectively less than,
  33. * equal to, or greater than the second. If two members
  34. * compare as equal, their order in the sorted array is
  35. * undefined.
  36. */
  37. int compare(const void *a,const void *b){
  38. const int *da = (const int *)a;
  39. const int *db = (const int *)b;
  40. return (*da>*db) - (*da<*db);
  41. }
  42.  
  43. int main(){
  44. long long int i,j,k,n,z;
  45. long long int unfairness=999999999999,new_unfairness;
  46. long long int min,max;
  47.  
  48. scanf("%lld",&n); //Input total number of packets
  49.  
  50. scanf("%lld",&k); //Input no. of children
  51.  
  52. int arr[n];
  53. //Input how much candy ith packet contains
  54. for(i=0;i<n;i++)
  55. scanf("%d",&arr[i]);
  56.  
  57. qsort(arr,n, sizeof(int),compare); //Sort in ascending order
  58.  
  59. for(i=0;i<n-k;i++) {
  60. z=k+i-1; // Z decides the size of new subarray
  61. if(z>(n-1)){ break;}
  62. min=arr[i]; // The minimum value of subarray is the first element
  63. max=arr[z]; // The maximum value of subarray is the last element
  64. new_unfairness = max-min; //Calculate the unfairness value
  65. if(new_unfairness<unfairness){ //Compare unfaireness value with previous one and see if it is less than old one
  66. //Update unfairness
  67. unfairness=new_unfairness; //If found less, then update old value with the new one.
  68. }
  69. }
  70. printf("%lld",unfairness);
  71. }
  72.  
  73. {% endhighlight %}
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement