Why do I get Wrong Answer?

,

The code is always wrong, I accept it exceeds time limit for second task, but it’s telling the answer wrong even for the subtask. Can anyone please help me out to figure out what’s wrong in this code.

Problem code: HMAPPY2- Appy and Contest

#include
using namespace std;

int main() {

int T;
long long int N,A,B,K,count=0;
std::cin >> T;
while(T--)
{
    cin>>N>>A>>B>>K;

for(int i=1;i<=N;i++)
{
    if((i>=A) && (i%B!=0) && (i%A==0))
        count++;
    if((i>=B) && (i%A!=0) && (i%B==0))
        count++;
}
if(count>=K)
    std::cout << "Win" << std::endl;
else
    cout<<"Lose";
}

return 0;
}

Even after changing the code multiple times and testing with different inputs still i get wrong answer. But it works find locally for a bunch of inputs i tried. Could anyone check and let me know whats wrong here ?

This answer is for next palindrome problem.

import java.io.*;

public class Main {

static int size;
static char[] n;

public static void main(String[] args) {

    try
    {
        Main m=new Main();
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        n = new char[1000002];

        int t;
        t = Integer.parseInt(input.readLine());

        while(t-- > 0)
        {
            n[0]='0';
            size=input.read(n,1,1000001);
            m.nextPalin();

            String temp= (n[0]=='0' ? String.valueOf(n,1,size) : String.valueOf(n,0,size+1)) ;
            System.out.print(temp);
        }
    }
    catch(Exception e)
    {

    }

}

void addNumber(int start, int end)
{
    int x,k;
    boolean needCarry=true;

    for(x=start;x>=end && needCarry ;x--)
    {
        if(n[x] == '9')
        {
            n[x]='0';
            needCarry=true;
        }
        else if(n[x] == '0')
        {
            n[x]='1';
            break;
        }
        else
        {
            k=Character.getNumericValue((n[x])+1);
            n[x]= (char)(k+'0');
            break;
        }
    }
}

void checkPalin()
{
    int i,j;

    for(i=1,j=size-1;i<j;i++,j--)
    {
        if(n[i]!=n[j])
            break;
    }
    if(i>=j)
        addNumber(size-1,0);
}

void nextPalin()
{
    checkPalin();
    int i,j;
    i =  n[0]=='0' ? 1: 0;

    for(j=size-1; i<j ;i++,j--)
    {
        if (n[i] < n[j])
        {
            addNumber(j-1,i);
            n[j] = n[i];
        }
        else if (n[i] > n[j])
        {
            n[j] = n[i];
        }

    }
}

}

Please tell me bout the error. Its a program to find the reverse n of the number.
#include
using namespace std;
int main()
{
int t;
cin>>t;
if((t>=1)&&(t<=1000))
{
for(int i=0; i<t; i++)
{
int num,rnum=0;
cin>>num;
if((num>=1)&&(num<=100000))
{
while(num!=0)
{
int b=num%10;
num=num/10;
rnum=rnum*10+b;
}
cout<<rnum<<endl;
}
}
}
}

what error is coming up?
by the way you didn’t wrote anything in #include at first line.

well sorry i messed up in pasting here although i addes iostream, error is wrong answer.

Please either format your code or (better!) link to your submission - the forum software has mangled it and it won’t compile! :slight_smile:

Also, please post a link to the Problem you’re trying to solve!

1 Like


I am facing same thing in other problems too, code is running, correct for the sample case but still its throwing wrong answer.

Can you please give me your submission link or your code so I can see what is wrong.

@sarthak1308 Your Code:

#include<iostream>
using namespace std;
int main()
{
	int t;
	cin>>t;
	if((t>=1)&&(t<=1000))
	{
		for(int i=0; i<t; i++)
		{
		int num,rnum=0;
		cin>>num;
		if((num>=1)&&(num<=100000))
		{
			while(num!=0)
			{
				int b=num%10;
				num=num/10;
				rnum=rnum*10+b;
			}
			cout<<rnum<<endl;
		}
	    }
	}
}

How is it affecting the logic…?
i got your point, it should be that way (more professional but logic…?)

Still it is throwing wrong output…

This change really shouldn’t make any difference - can you give a testcase where it does?

I can’t see anything wrong with @sarthak1308’s solution at all, provided that the constraints are correct, which they often aren’t.

@sarthak1308 - try removing the if((t>=1)&&(t<=1000)) and if((num>=1)&&(num<=100000)) checks (they are not needed anyway). Then submit, and post a link to your submission here.

@vijju123 / @admin - can we get the constraints/ testcases fixed for this Problem?

Thanks to @aryan12 for confirming :slight_smile:

4 Likes

Gotcha! You have to remove the Constraints part.
Here my solution:

#include<iostream>
using namespace std;
int main()
{
	int t;
	cin>>t;
	if((t>=1)&&(t<=1000))
	{
		for(int i=0; i<t; i++)
		{
		int num,rnum=0;
		cin>>num;
			while(num>=10)
			{
				int b=num%10;
				rnum=rnum*10+b;
				num=num/10;
			}
			rnum=rnum*10+num;
			cout<<rnum<<endl;
		}
	}
}
1 Like

I don’t know, but I have just removed these two lines and I have got AC

2 lines I have removed

if((t>=1)&&(t<=1000))

if((num>=1)&&(num<=100000))

2 Likes

Shit! After I understand it and post my code for help, I saw @ssjgz already post this.

2 Likes

Thank you so much…its working after i removed the constraints…

3 Likes

The test file have N \leq 10^6. I have modified the statement. :slight_smile:

2 Likes

Great, thanks for the rapid fix :slight_smile:

1 Like

Lead game problem
Can you help me out here with whats wrong…Wrong answer error
#include
using namespace std;
int main()
{
int T;
int alead=0,blead=0;
cin>>T;
if (T<=1000)
{
for (int i=0; i<T; i++)
{
int ascore,bscore;
cin>>ascore>>bscore;
if (ascore>bscore)
{
if((ascore-bscore)>alead)
alead=ascore-bscore;
}
else if (bscore>ascore)
{
if((bscore-ascore)>blead)
blead=bscore-ascore;
}
}
if(alead>blead)
cout<<1<<" “<<alead;
else if(blead>alead)
cout<<2<<” "<<blead;
}
}