You are not logged in. Please login at to post your questions!


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 i am beginner to this plz help!

asked 24 Dec '18, 16:28

tsar1's gravatar image

accept rate: 0%

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.


answered 24 Dec '18, 17:39

masood786's gravatar image

accept rate: 13%

i tried this but it didn't worked here is the link of my solution

(24 Dec '18, 18:04) tsar12★

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

(24 Dec '18, 18:06) masood7864★

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

(24 Dec '18, 19:35) tsar12★

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

(24 Dec '18, 19:38) masood7864★

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.

(24 Dec '18, 19:41) masood7864★

yes sir i did the exact thing as you said my solution

(24 Dec '18, 19:48) tsar12★

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

(24 Dec '18, 19:49) masood7864★

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

(24 Dec '18, 19:51) masood7864★

sir i did recommended changes still getting WA

(24 Dec '18, 20:08) tsar12★

There's a problem with your logic.

(24 Dec '18, 20:10) masood7864★

ok thanks sir for helping me

(24 Dec '18, 20:11) tsar12★

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.

(24 Dec '18, 20:13) masood7864★

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.

(24 Dec '18, 20:14) masood7864★

Just a suggestion --

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

(24 Dec '18, 20:18) masood7864★
showing 5 of 14 show all

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


answered 24 Dec '18, 20:35

masood786's gravatar image

accept rate: 13%

I am trying the above mentioned method

(24 Dec '18, 20:57) tsar12★

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.

(24 Dec '18, 21:02) tsar12★

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.

(24 Dec '18, 21:05) masood7864★

Thanks a lot sir you helped me a lot.

(24 Dec '18, 21:08) tsar12★

Just do this

scanf("%d",&n); int arr[n]

and your array would be ready.

(24 Dec '18, 21:09) masood7864★

Just a question if i have to make a if else statement if someone gives input other than 1 and 0

(24 Dec '18, 21:10) tsar12★

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.

(24 Dec '18, 21:15) masood7864★

So, did you solve it finally?

(24 Dec '18, 21:15) masood7864★

new error is encountered called time exceeded solution

(24 Dec '18, 21:42) tsar12★

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.

(24 Dec '18, 21:51) masood7864★

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?

(24 Dec '18, 22:00) tsar12★
(24 Dec '18, 22:03) tsar12★

Well, you can mail me at

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!

(24 Dec '18, 22:06) masood7864★

thank you sir but can you please tell me how can i improve it so that i can work with it

(24 Dec '18, 22:08) tsar12★

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.

(24 Dec '18, 22:19) masood7864★
showing 5 of 15 show all
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:


question asked: 24 Dec '18, 16:28

question was seen: 271 times

last updated: 24 Dec '18, 22:19