Street Parade SPOJ

import java.util.*;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(true){
int n = sc.nextInt();
if(n==0)
break;
int arr[] = new int[n];
for(int i=0;i<n;i++){
arr[i] = sc.nextInt();
}
Stack st1 = new Stack();
Stack st2 = new Stack();
int pos = 1;
for(int i=0;i<n;i++){
if(arr[i] == pos){
st2.push(arr[i]);
pos++;
}
else{
st1.push(arr[i]);
}
}
while(!st1.empty()){
if(pos==(int)st1.peek()){
st1.pop();
st2.push(pos);
pos++;
}
else{
st1.pop();
}
}
if(st2.size()==n){
System.out.println(“yes”);
}
else
System.out.println(“no”);
}
}
}

NO idea where I am going wrong. Passed basic test cases. WA on submission.

GOT IT! I missed a basic possibilty

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