Created
March 23, 2017 20:38
-
-
Save asclines/9e5a829b26744ebc023b0446f0c143bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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