1
0
Fork 0
mirror of https://gitlab.com/mfocko/CodeWars.git synced 2024-09-19 14:16:55 +02:00
CodeWars/4kyu/twice_linear/solution.js
Matej Focko fc899b0b02
chore: initial commit
Signed-off-by: Matej Focko <mfocko@redhat.com>
2021-12-28 16:19:58 +01:00

31 lines
628 B
JavaScript

function dblLinear(n) {
let values = [1];
let seen = new Set(values);
let index = 0;
let length = 0;
while (length < n) {
const x = values.shift();
seen.delete(x);
const a = 2 * x + 1;
const b = 3 * x + 1;
if (!seen.has(a)) {
let i = index;
for (;i < values.length; i++)
if (values[i] > a)
break;
values.splice(i, 0, a);
seen.add(a);
index = i;
}
seen.add(b);
values.push(b);
length++;
}
console.log(values);
return values[0];
}