Hello everyone, I have tried to solve the STPAR in both Java and C++, the logic is still same at both, but with Java, I have Wrong Answer when submitting the code. The logic is ok, I think there is a problem with stdin but I don’t know how to fix it. Here is my code, please help me, thank you so much!!
import java.util.Scanner;
public class StreetParade {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int mainStackOrder[] = new int[1000];
int sideStackTemp[] = new int[1000];
/*
* Sample input:
5
5
1 2 4 3
0
*/
while (true) {
// I think the error start from here @@
int n = scn.nextInt();
if (n == 0)
break;
int sideStackSize = 0;
// and here
for (int i = 0; i < n; i++) {
mainStackOrder[i] = scn.nextInt();
}
int startIndex = 1;
boolean check = true;
for (int i = 0; i < n; i++) {
if (sideStackSize > 0 && sideStackTemp[sideStackSize - 1] == startIndex) {
sideStackSize--;
startIndex++;
}
if (mainStackOrder[i] == startIndex) {
startIndex++;
}
else if (sideStackSize > 0 && sideStackTemp[sideStackSize - 1] < mainStackOrder[i]) {
check = false;
break;
} else {
sideStackTemp[sideStackSize] = mainStackOrder[i];
sideStackSize++;
}
}
if (check == true) {
System.out.println("yes");
} else {
System.out.println("no");
}
}
}
}