1
0
Fork 0
mirror of https://gitlab.com/mfocko/CodeWars.git synced 2024-11-22 08:33:48 +01:00
CodeWars/4kyu/twice_linear/solution.js

32 lines
628 B
JavaScript
Raw Permalink Normal View History

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];
}