-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathb2485.js
More file actions
33 lines (26 loc) · 688 Bytes
/
b2485.js
File metadata and controls
33 lines (26 loc) · 688 Bytes
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
var fs = require("fs");
var input = fs.readFileSync("/dev/stdin").toString().trim().split("\n");
const trees = input.slice(1).map(Number);
const gaps = getTreeGaps(trees);
const gcd = gaps.reduce(getGCD);
const dist = trees[trees.length - 1] - trees[0];
const fills = dist / gcd - 1;
console.log(fills - (trees.length - 2));
function getTreeGaps(trees) {
const gaps = [];
for (let i = 0; i < trees.length - 1; i++) {
gaps.push(trees[i + 1] - trees[i]);
}
return gaps;
}
function getGCD(a, b) {
let big = a > b ? a : b;
let small = a <= b ? a : b;
let tmp;
while (big % small > 0) {
tmp = small;
small = big % small;
big = tmp;
}
return small;
}