Skip to content

Instantly share code, notes, and snippets.

@asclines
Created March 23, 2017 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asclines/9e5a829b26744ebc023b0446f0c143bb to your computer and use it in GitHub Desktop.
Save asclines/9e5a829b26744ebc023b0446f0c143bb to your computer and use it in GitHub Desktop.
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