Please check where I am wrong.
Prateek sir and coding(testcases not passing)
Hello @ashish_meher,
There are few issues with your logic:
- Confusion due to 0,
- What would be the output of your code, if the stack goes empty somewhere in the middle of processing.
Let’s understand this with an example:
10
1
2 83
2 43
2 16
2 0 //point (1)
1
1
1 //point(2)
2 48
1
Expected Output:
No Code
0
16
43
48
(As there are 5 1 in the input)
Your Output:
No Code
16 //one extra output due to point(1)
43
83
0 //Point(2), it should have printed “No Code”
48
BTW, why are you using array?
Why don’t you use the stack directly:
-
for each 1,
…Check the stack.
…(A) If it is empty, print “No Code”
…(B) else, pop the top element of stack. -
for each 2,
…Push the element into the stack.
This, is the simplest approach.
Same you have to implement in the code you have written.
Hope, this would help.
If you still have doubts, let me know.
Give a like, if you are satisfied.
1 Like