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 :
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
Understand the input format correctly. It is something of this kind…
while N is not 0
// your code here
You are not given number of test cases. Test file ends with N = 0 where you should terminate your code.
With this test:
1 2 3 4 5
5 4 3 2 1
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
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…
Now i am bit confused about the input and output…
Can you please explain me what will be the output for this test case
5 4 3 2 1
3 2 1
Yes for both the cases.
Explanation for 2nd case…
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.
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…