WA in COMPILER despite passing sample and various other cases


Here is my solution: CodeChef: Practical coding for everyone

Can you please give some test cases that fail this and explain why it fails, since it maybe that I have missed some nuances in the question itself.

My logic → As soon the number of ‘>’ gets more than number of ‘<’ encountered going left to right, break. Then check whether you encountered equal number of ‘<’ and ‘>’ till then.


Your solution gives the incorrect output for the following test input:


The answer should be 2. This is because the prefix of <>< of length 2 - that is:


is valid, but no larger prefixes of <>< are.

1 Like

Hey if your count_a becomes more than count_b and the loop terminates then the outer
if statement i.e.
cout << result << endl;
won’t work and the answer printed out will always be 0.

Hope this clarify your doubt :slight_smile:

Thank you so much. I understood the issue. Rewrote the solution, but used Stack this time and got AC (CodeChef: Practical coding for everyone). Thank you @ssjgz and @daniyal1998.

1 Like

You really don’t need to use stack data structure explicitly, you can use logic of the way stack works to solve the problem.

Refer to another solution: Competitive-Programming/Code-Chef/Compiler-and-Parsers at master · strikersps/Competitive-Programming · GitHub.

1 Like