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

32 lines
628 B
JavaScript
Raw 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];
}