Problem Link : http://www.spoj.com/problems/STPAR/

I have tried debugging my code but i am unable to find the error. Please Help.!!

My Actual Solution Link: http://ideone.com/kJ6rDr

Modified Solution Link [with multiple random generated test cases] : http://ideone.com/kECT3L

My Pseudo-code :
Getting Input
for i from 1 to top: //top=size of array;
for j from k to top:
Checking if stack[j] == i:
If true j++ ,k=j and break
Else Check queue[index]==i:
if true index-- and break
Else enqueue stack[j] at queue[++index]:
Check whether queue[index]>queue[index-1]:
if true break and print no

Please Help!!

Understand the input format correctly. It is something of this kindâ€¦

```
read(N)
while N is not 0
read Array
// your code here
read(N)
```

You are not given number of test cases. Test file ends with N = 0 where you should terminate your code.

1 Like

With this test:

5

1 2 3 4 5

5

5 4 3 2 1

0

What is your answer? It got TLE because you read wrong integer. No number of tests are given in test, but if you read â€ślove of mobilesâ€ť is equal 0, it will terminate

I have answered the question about this problem before. Here is my algorithm for that problem:

Let assume x is current number, and we have one stack stores a â€ślove mobilesâ€ť which couldnâ€™t be in-ordered at sometimes.

If stack[top]=x, then pop it from stack and increase x by one. Repeat that process until stack[top]<>x or stack is empty.

If x=a[i], then increase x by one, else push it into stack. Actually, increase i. Repeat that process until (i or i) >number of love mobiles.

If we canâ€™t do any process above, and x is not exceed number of love mobiles, we output â€śnoâ€ť, else â€śyesâ€ť.

Here is my sample solution for that problem: #12991927 on SPOJ

1 Like

Seems i misunderstood the questionâ€¦

Thanks @vinayawsm for pointing that outâ€¦

But still have some confusions, which i have mentioned in @leduongtuananh 's commentâ€¦

Can you please Explainâ€¦

Thanksâ€¦

Now i am bit confused about the input and outputâ€¦

Hey @leduongtuananh

Can you please explain me what will be the output for this test case

5
5 4 3 2 1
3
3 2 1
0

Pleaseâ€¦ !!

Yes for both the cases.

Explanation for 2nd caseâ€¦

```
3
3 2 1
```

3 enters side street.

2 enters side street.

1 moves to other side.

2 moves to other side.

3 moves to other side.

They are in order now.

1 Like

okay so you mean those were two different casesâ€¦

I thought that we have to take both into consideration to solve a single test caseâ€¦

Now i got itâ€¦ Thanksâ€¦