-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathB2-B.js
More file actions
32 lines (26 loc) · 909 Bytes
/
B2-B.js
File metadata and controls
32 lines (26 loc) · 909 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
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let inputArray = [];
rl.on("line", (input) => {
inputArray = input.split(" ").map((i) => parseInt(i));
rl.close();
});
rl.on("close", () => {
console.log(getMaxNearestDistance(inputArray));
});
function getMaxNearestDistance(array, form = 1, to = 2) {
let maxDistance = 0;
for (let i = 0; i < array.length; i++) {
if (array[i] === form) {
const leftMarketIndex = array.lastIndexOf(to, i);
const leftMarketDistance = leftMarketIndex !== -1 ? i - leftMarketIndex : Infinity;
const rightMarketIndex = array.indexOf(to, i);
const rightMarketDistance = rightMarketIndex !== -1 ? rightMarketIndex - i : Infinity;
maxDistance = Math.max(maxDistance, Math.min(leftMarketDistance, rightMarketDistance));
}
}
return maxDistance;
}