STPAR - Street Parade

For sure, the love mobiles will roll again on this summer's street parade. Each year, the organisers decide on a fixed order for the decorated trucks. Experience taught them to keep free a side street to be able to bring the trucks into order.

The side street is so narrow that no two cars can pass each other. Thus, the love mobile that enters the side street last must necessarily leave the side street first. Because the trucks and the ravers move up closely, a truck cannot drive back and re-enter the side street or the approach street.

You are given the order in which the love mobiles arrive. Write a program that decides if the love mobiles can be brought into the order that the organisers want them to be.


There are several test cases. The first line of each test case contains a single number n, the number of love mobiles. The second line contains the numbers 1 to n in an arbitrary order. All the numbers are separated by single spaces. These numbers indicate the order in which the trucks arrive in the approach street. No more than 1000 love mobiles participate in the street parade. Input ends with number 0.


For each test case your program has to output a line containing a single word "yes" if the love mobiles can be re-ordered with the help of the side street, and a single word "no" in the opposite case.


Sample input:
5 1 2 4 3 

Sample output:


The sample input reflects the following situation:

The five trucks can be re-ordered in the following way:

hide comments
Rafail Loizou: 2021-05-09 20:03:38

you don't even need a queue as many say in the comments.
Also multiple cases be careful.

makhan_28: 2021-04-18 14:53:28

One stack and one queue :)

harsh_arora: 2021-03-05 16:50:38

don't take t as input for test just do
if(n == 0)break;
//your code

utkarsh0124: 2021-03-04 08:32:31

Try it yourself first
for those who couldn't figure out here's the code: <snip>

[NG]: This is a site for ppl who can and want to figure out. For those who can't, there's funny cats and instagram to browse. Don't post any code here.

Last edit: 2021-03-04 11:50:13
yasser1110: 2021-02-20 16:14:38

Solved it just using an array. But had to make multiple attempts because I missed that there could be multiple sets of inputs with 0 coming at the very end.

chalapathi_444: 2021-01-12 05:02:54

Worked on this problem 4 hours, thinking that no more than 2 cars are accommodated in side-street.
Thank you one of the commenters providing a good test case.

lakshmi116: 2021-01-10 13:58:12

the question is simple. please try again if you fail, also check whether you are iterating through the array correctly.

arafat_123: 2020-12-20 16:04:04

Got AC in one go. Yesssss.

lamda_cdm_10: 2020-12-15 06:14:04

Just one warning ! There are several test cases and not a single one and '0' comes at the end of all.

itzsowvik: 2020-12-11 12:46:34

2 1 3 5 4

Helped me to solve this

Last edit: 2020-12-11 12:46:52

Added by:Patryk Pomykalski
Time limit:1s-2s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: NODEJS PERL6 VB.NET
Resource:Swiss Olympiad in Informatics 2004