AMR10G - Christmas Play

My kid's kindergarten class is putting up a Christmas play.  (I hope he gets the lead role.)  The kids are all excited, but the teacher has a lot of work.  She has to produce costumes for a scene with K soldiers.  She wants to buy all the costumes in the same size, allowing for some small amount of length alteration to be done by the kids' parents later.  So she has taken all the kids' height measurements.  Can you help her select K kids from her class of N to play the soldier role, such that the height difference between the tallest and shortest in the group is minimized, and alternations will be easiest?  Tell her what this minimum difference is.
 
INPUT
The first line contains the number of test cases T. T test cases follow each containing 2 lines.
 
The first line of each test case contains 2 integers N and K.
The second line contains N integers denoting the height of the N kids.
 
OUTPUT
Output T lines, each line containing the required answer for the corresponding test case.
 
CONSTRAINTS
T <= 30
1 <= K <= N <= 20000
1 <= height <= 1000000000 

SAMPLE INPUT
3
3 1
2 5 4
3 2
5 2 4
3 3
2 5 4
SAMPLE OUTPUT
0
1
3
EXPLANATION
In the first test case, the teacher needs to only select 1 kid and hence she can choose any kid since the height difference is going to be 0.
In the second test case, the teacher can choose kids with height 4 and 5.
In the third test case, the teacher is forced to choose all 3 kids and hence the answer = 5-2 = 3


Added by:Varun Jalan
Date:2010-12-13
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:ICPC Asia regionals, Amritapuri 2010

hide comments
2016-06-21 07:44:12
My 50th!!!☺
2016-06-18 14:28:59
finally got it!!!!....easy question....no tricky test cases.
best timed solution in python (y)


Last edit: 2016-06-18 14:41:30
2016-06-14 11:20:25
Are there any tricky test cases ?
My code is giving correct answer for the test cases given in the forums.

Last edit: 2016-06-14 11:30:07
2016-02-27 16:14:11
are the individual heights distinct?
2016-02-05 15:06:11
java users do use fast i/o for (nlogn +n)
2016-01-30 19:00:26
use stl for sorting and long long instead of double
2016-01-26 18:32:06 Devashish Mathur
Initially took a completely different track... Got ac in second try..
2016-01-26 18:22:12
stl ;) sorting
2016-01-26 10:49:13
simple way to overcome TLE.
to sort, use the headerfile #include<algorithm>
and the sorting procedure is, sort(arrayname,arrayname+numberofelements);
works like a dream.
2016-01-24 06:01:26
java giving TLE :(... C++ not!
that's not fair spoj!
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.