3 / 3
Mar 2019

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

}

  • created

    Mar '19
  • last reply

    Mar '19
  • 2

    replies

  • 1.0k

    views

  • 2

    users

  • 1

    like

try these:

10
1 2 10 5 4 3 7 6 8 9
10
1 2 10 5 4 3 9 8 7 6
0

Answer should be yes for both.

I tried them, both I have NO
So, may be I’m wrong in somewhere, thank you so much for reply! :like: