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.
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 T lines, each line containing the required answer for the corresponding test case.
T <= 30
1 <= K <= N <= 20000
1 <= height <= 1000000000

3 1
2 5 4
3 2
5 2 4
3 3
2 5 4
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

hide comments
anuj0503: 2016-01-15 18:59:55

sorting !!

dokz: 2016-01-15 06:58:51

Getting a runtime error without a reason on C#. At the same time, it runs great on my PC and ideone.

Update: Never mind. There were some extra spaces in the input, AC after Trim().

Last edit: 2016-01-15 07:00:41
Junaid: 2016-01-11 10:30:54

Same logic in JAVA:- TLE in C++:-AC...don't know why...

damsehgal: 2016-01-03 17:20:22

typo costed me WA
never mind easy question.

sarangs: 2015-12-26 15:57:48

Should be moved to tutorial....too easy

BRAIN: 2015-12-26 04:04:53

O(nlogn + n)

Vineeth Reddy: 2015-12-20 15:17:16

was getting TLE then changed logic to O(logn+n) AC:) My 50th

Ravi Chandra: 2015-09-21 18:25:09

Easy One

Shivam Singh: 2015-08-20 14:36:51

(i+K)<N instead of (i+K)<=N was causing WA :P
the last set was getting considered, silly mistake

Sumit Paroothi: 2015-08-12 07:27:05

easy one!

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