Why do I get Wrong Answer?

,

#include
#include <math.h>

using namespace std;

bool isPerfectSquare(int x)
{
int s = sqrt(x);
return (s*s == x);
}

bool isFibonacci(int n)
{

return isPerfectSquare(5*n*n + 4) || 
       isPerfectSquare(5*n*n - 4); 

}

int main()
{
int t;
cin >> t;
for(int i = 0; i < t; i++){
int a;
cin >> a;
if(isFibonacci(a)){
cout << “YES” <<endl;
}
else{
cout << “NO” << endl;
}
}
return 0;
}
it is showing wrong answer, but it works fine on my machine . please help.

so lets see
you want us to guess the ques, the expected ouput format, ur logic
after that debug ur code
well most of us do not read mind

AMIFIB Problem - CodeChef heres the link to the problem.

i dont want anyone to solve that ques for me , it works fine on my machine, just here on codechef it is showing wrong answer

how did u check ur prog?
did u jst check the given cases ?
coz thats not enough
and why u did not jst calc the Fibonacci series then check if a num exist or not ?
if abv brute force prob gives TLE (which i highly doubt it will if implemented properly) still you can use it to debug ur logic with formula

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