WA, cannot guess the test case

submission link
https://www.codechef.com/submit/VALIDSTK

you could have tested your code sample test case given in problem statement page…

you can not take N numbers input into a string in c++.

you would have to declare a vector/array of variables and then take input using a loop.

the problem is with the string.

https://www.codechef.com/viewsolution/35616460

you submission link is not working i had to search from your profile for you submission. is this your submission?

yes

what is the length string can store ?
value of N

problem is input format is space separated integers

example : 1 0 1 1 1 0 1 0 1

if you try to take input as cin >> s

s will be assigned only characters till there is a space, there fore , s = "1" after cin.

here is your code after little modifications … https://www.codechef.com/viewsolution/35616541

hope this would help you understand .

+point : your logic was also as you were checking for invalid popping iteration and not at the time of pop.

1 Like
                if (valid < 1)  
                {
                    invalid = -1;
                    break;
                }

if value of the stack is 0 means no element, but it is valid, if the value of the stack is -1 means invalid.
is i am correct

yes. number of valid values in stack need to strictly greater than 0 at the time of popping element. not after popping in next iteration.

i understand your logic,
if at the beginning we get ‘0’ then the stack goes to -1 and hence invalid.
else we increment valid and keep on computing.

your code was not wrong there, if first element is ‘0’, then in next iteration your code would have ouputed invalid.

error was in for input like these : 1 1 1 1 0 0 0 0 0
if you try to map out what your code was doing, then you would find out the in the last iteration of your code, valid’s value would go below 0, and there was no check in that iteration for that invalid.

so code would have outputted Valid, while it was Invalid.

how to quess test cases

pick up pen and paper, make boxes for your variables and keep them updated surely you can find all cases for ad-hoc / beginners problem like these on a piece of paper. our brains are intelligent but not efficient.