forked from rajatgoyal715/Hackerrank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaxMin.java
32 lines (24 loc) · 920 Bytes
/
MaxMin.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
// The part of the program involving reading from STDIN and writing to STDOUT has been provided by us.
public class Solution {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(in.readLine());
int K = Integer.parseInt(in.readLine());
int[] list = new int[N];
for(int i = 0; i < N; i ++)
list[i] = Integer.parseInt(in.readLine());
int unfairness = Integer.MAX_VALUE;
Arrays.sort(list);
int temp;
for(int i=0;i<=N-K;i++){
temp = list[i+K-1]-list[i];
if(temp<unfairness)
unfairness = temp;
}
System.out.println(unfairness);
}
}