Problem: Angry Children

Bill Gates is on one of his philanthropic journeys to a village in Utopia. He has Npackets 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 a fight between the children, he would like to pick K out of N packets such that the 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?

Input Format
The first line contains an integer N.
The second line contains an integer K. N lines follow each integer containing the candy in the ith packet.

Output Format
A single integer which will be the minimum unfairness.

Constraints
1<=N<=105
1<=K<=N
0<= number of candies in any packet <=109

Sample Input #00

7
3
10
100
300
200
1000
20
30

Sample Output #00

20

Explanation #00
Here K = 3. We can choose packets that contain 10,20,30 candies. The unfairness is

max(10,20,30) - min(10,20,30) = 30 - 10 = 20

Sample Input #01

10
4
1
2
3
4
10
20
30
40
100
200

Sample Output #01

3

Explanation #01
Here K = 4. We can choose the packets that contain 1,2,3,4 candies. The unfairness is

max(1,2,3,4) - min(1,2,3,4) = 4 - 1 = 3

This is my code:

#include <stdio.h>

void QuickSort(long *a, long l,long r);

int main(){
long N,K,i,min=999999999,temp=0;
scanf(“%ld%ld”,&N,&K);
long a[N];
for(i=0;i<N;++i)
scanf(“%ld”,&a[i]);

QuickSort(a,0,N-1);
for(i=0;i<=N-K;++i){
temp=a[i+K-1]-a[i];
if(temp<min)
min=temp;
}
printf(“%ld”,min);
return 0;
}

void QuickSort(long *a,long l,long r){
long x,temp;
long i,j;
x=a[(l+r)/2];
i=l;j=r;
do{
while(a[i]<x) i++;
while(a[j]>x) j–;
if(i<=j){
temp=a[i];
a[i]=a[j];
a[j]=temp;
i++; j–;
}
}while(i<j);
if(l<j)
QuickSort(a,l,j);
if(i<r)
QuickSort(a,i,r);
}

Advertisements