-
Notifications
You must be signed in to change notification settings - Fork 2
/
angry-children.js
45 lines (37 loc) · 1.02 KB
/
angry-children.js
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
33
34
35
36
37
38
39
40
41
42
43
44
45
process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
input_stdin += data;
});
process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});
function readLine() {
return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
function angryChildren(k, arr) {
// Complete this function
arr.sort((x, y) => x - y);
let min = arr[arr.length - 1];
for (let i = 0; i < arr.length - k + 1; i++) {
if (arr[i + k - 1] - arr[i] < min) {
min = arr[i + k - 1] - arr[i];
}
}
return min;
}
function main() {
var n = parseInt(readLine());
var k = parseInt(readLine());
var arr = [];
for(var arr_i = 0; arr_i < n; arr_i++){
arr[arr_i] = parseInt(readLine());
}
var result = angryChildren(k, arr);
process.stdout.write("" + result + "\n");
}