function generateFirstHalfPreservedSequence(num){
const halfsParity = [];
let length = num;
while(length > 1){
halfsParity.push((length % 2) !== 0);
length = Math.floor(length / 2) + length % 2;
}
const stack = [1];
let runningNum = 1;
while(halfsParity.length > 0){
const isThisHalfOdd = halfsParity.pop();
let stackLength = stack.length;
if(isThisHalfOdd) stackLength--;
while(stackLength--){
const frontElement = stack.shift();
stack.push(frontElement);
stack.push(++runningNum);
}
if(isThisHalfOdd){
const frontElement = stack.shift();
stack.push(frontElement);
}
}
return stack;
}
console.log(generateFirstHalfPreservedSequence(10));