Skip to content

Commit

Permalink
Merge pull request #159 from algo-malgo/algorithm/#159
Browse files Browse the repository at this point in the history
2485번 가로수
  • Loading branch information
NamJwong authored May 10, 2023
2 parents f5cb87d + 132dbe8 commit 9949d30
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
5 changes: 5 additions & 0 deletions BOJ/2485 가로수/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
4
2
6
12
18
43 changes: 43 additions & 0 deletions BOJ/2485 가로수/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const solution = (numbers) => {
const findGCD = (numbers) => {
let gcd = numbers[0];

for (let i = 1; i < numbers.length; i++) {
gcd = getGCD(gcd, numbers[i]);
}

return gcd;
};

const getGCD = (a, b) => {
while (b !== 0) {
let temp = b;
b = a % b;
a = temp;
}

return a;
};

let answer = 0;

const gaps = [];
for (let i = 1; i < numbers.length; i++) {
gaps.push(numbers[i] - numbers[i - 1]);
}

const gcd = findGCD(gaps);

gaps.forEach((gap) => {
answer += gap / gcd - 1;
});

return answer;
};

const fs = require('fs');
const file = process.platform === 'linux' ? '/dev/stdin' : './input.txt';
const inputs = fs.readFileSync(file).toString().trim().split('\n');
const [N, ...numbers] = inputs;

console.log(solution(numbers.map(Number)));

0 comments on commit 9949d30

Please sign in to comment.