Skip to content

Instantly share code, notes, and snippets.

@asclines
Created Mar 23, 2017
Embed
What would you like to do?
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
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);
for(int i = 0; i <= N-K; i++) {
if(list[i+K-1] - list[i] < unfairness) {
unfairness = (list[i+K-1] - list[i]);
}
}
System.out.println(unfairness);
}
}
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment