i am getting the wrong answer but my output is right

i don’t know whats wrong with my code i am getting wrong answer
here is link of my solution https://www.codechef.com/viewsolution/22074540
i am beginner to this plz help!

In competitive programming, you do not need to print unnecessary strings like Enter the number of operations, Enter the number of test cases, etc. It would result in a WA verdict if you do so.

Just remove the extra printf statements from your code and stick to the output guidelines in the question and hopefully, you’ll get an AC verdict.


i tried this but it didn’t worked
here is the link of my solution https://www.codechef.com/viewsolution/22075473

Your printing format is wrong. Check the question description again.

Sir i have checked and changed my printing format as you said there is still problem of wrong answer

I meant that the printing format in your new code is wrong too. (Post the updated link if you made any new submissions.)

You are required to print as Valid/n (Note That V is in capitals) and Invalid/n(Note that I is capitalized).

You are printing it as /n valid and /n Invalid which is wrong.

yes sir i did the exact thing as you said
my solution https://www.codechef.com/viewsolution/22076311

I see that you didn’t define main as int!
That’s a compiler error. Correct it.

Moreover, in the last for loop, why are you checking if top<-1 without any queries

sir i did recommended changes still getting WA

There’s a problem with your logic.

ok thanks sir for helping me

Here’s an outline of what you need to do.

First, set top to 0

Now, store all queries in an array

Traverse the array, if the query is push, simply increment top.

If the query is pop,
Check whether stack is empty or not (by checking if top==0)
If the stack is empty, print invalid and terminate.
Else decrease top by 1
(This was the part which you did wrong).

At the end of the loop, print Valid.

Use meaningful variable names. Avoid using single letter variables unless it has very less scope (as in the iterator of for loop).

Avoid using the variable o which can easily be confused with 0.

Just a suggestion –

Don’t use pointers and malloc when starting out with competitive programming. The code can become quite clumsy.

Do you need help in debugging your original solution or are you trying out the method that I posted in the comments?

I am trying the above mentioned method

Sir i used dynamic memory allocation just to stop memory wastage because i don’t know how long the operation 1.0 will be given by computer.If i use static memeory allocation then i have to declare an array of very large size.

That’s not correct. You know exactly how many inputs you’ll be getting.

For each test case, there will be n inputs. So, just make an array of size n after you have scanned n.