Hackerrank solution for Angry Kids problem in C language
18 September 2014By Bhavyanshu Parasher
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.
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
max(x1,x2,…xk) - min(x1,x2,…xk)
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?
The first line contains an integer N.
The second line contains an integer K. N lines follow. Each line contains an integer that denotes the candy in the ith packet.
An integer that denotes the minimum possible value of unfairness.
Program that works for all test cases
(Compiled & Tested on Linux with GNU GCC)
The code can be found in my github repository of RandomAlgorithms with 2 testcases for this problem. If any doubt or suggestion, then leave a comment below.