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.
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.
Thanks a lot sir you helped me a lot.
Just do this
scanf("%d",&n);
int arr[n]
and your array would be ready.
Just a question if i have to make a if else statement if someone gives input other than 1 and 0
Well yeah, you could do that, but that’s just overkill. Be assured that there won’t be any inputs other than 0 , 1 . Unless it is mentioned specifically in the question, illegal inputs won’t be present in competitions.
So, did you solve it finally?
It’s because you are using the same variable i for all the for loops which is messing up the conditions leading to TLE. Use different variables for each for loop.
did it sir finally ,it won’t happen if you didn’t helped me
if had to ask you in future directly how can i ask?
Well, you can mail me at masood@iitk.ac.in
And yeah, I’d suggest you to improve upon the basics of implementation and input/output before jumping on to solving questions from contests. Good luck!
thank you sir but can you please tell me how can i improve it so that i can work with it
I started coding on GeeksForGeeks first. It has well written solutions of all the problems along with proper comments and formatting. If you wish, you could learn the coding style from there.
All this is based on personal experience and if you wanna improve your basics from CodeChef, it’ s fine too.