I have a Javascript code that is working fine…
function arrangeCars(arr,n) {
let stack = [];
let count = 1;
while(arr.length || stack.length) { // IMPORTANT QUESTION
if(arr.length && !stack.length) { // USE SIDE LANE / STREET PARADE / ARRANGE CARS
if(count == arr[0]) {
count++;
arr.shift();
}else{
stack.push(arr.shift());
}
}
else if(arr.length && stack.length) {
if(count == arr[0]) {
arr.shift();
count++;
}
else if(count == stack[stack.length-1]) {
stack.pop();
count++;
}else {
stack.push(arr.shift());
}
}
else if(!arr.length && stack.length) {
if(count == stack[stack.length-1]) {
stack.pop();
count++;
}else {
console.log("NO");
break;
}
}
}
if(arr.length == 0 && stack.length == 0) {
console.log("YES");
}
}
function runProgram(input){
input = input.trim().split("\n");
let t = +input[0].trim();
let line = 1;
for(let i = 0;i<t; i++){
let n = +input[line].trim();
line++;
let arr = input[line].trim().split(" ").map(Number);
line++;
arrangeCars(arr,n)
}
}
if (process.env.USERNAME === “”) {
runProgram(``);
} else {
process.stdin.resume();
process.stdin.setEncoding(“ascii”);
let read = “”;
process.stdin.on(“data”, function (input) {
read += input;
});
process.stdin.on(“end”, function () {
read = read.replace(/\n$/, “”);
read = read.replace(/\n$/, “”);
runProgram(read);
});
process.on(“SIGINT”, function () {
read = read.replace(/\n$/, “”);
runProgram(read);
process.exit(0);
});
}