Why do I get Wrong Answer?

,

I am able to get all the answers for the sample inputs given in the problem. But still I get “Wrong answer”.
my problem code is :: CHEFKEY and my answer code is below .

/*
			-----   Kiamottullah   -----
			       (20/ 10/ 2016)
			All code is written by me .
			You can allways use this code for
			Learning context . keep learning

*/

#include
#include
using namespace std;

class Pair{
private :
long long int X;
long long int Y;

public :
    Pair(int x, int y){
        X = x;
        Y = y;       // constructor
    }
    		// prototypes of this class
    int getX (){
        return X;
    }
    int getY (){
        return Y;
    }
    void show (){
        cout << getX() << " " << getY() << endl;
    }

};

class Testcase{
private :
int N; // height
int M; // width
long long int C; // number of colours

    vector <Pair> Pairs;    // it's pairs
    
public :
    Testcase (int n, int m, int c){
        N = n;
        M = m;      // constructor
        C = c;
        calculatePairs();
    }
    
    		// prototypes of this class
    int getN (){
        return N;
    }
    int getM (){
        return M;
    }
    int getC (){
        return C;
    }
    
    void calculatePairs (){
    // is all color (C) fit in (n*m) display
        if((N * M) >= C){
        
        	// firstly push two pairs (1, C) and (C, 1)
            int x = 1, y = C;
            if(isFit(x, y)){
                Pairs.push_back(Pair(x, y));
            }
            x = C, y = 1;
            if(isFit(x, y)){
                Pairs.push_back(Pair(x, y));
            }
            // checking all the pairs from 2 to C/2
            int hulf = C/2;
            for(int i = 2; i <= hulf; i++){		
                for(int j = 2; j <= C/i; j++){
                    if(isPair(i, j)){
                        if(isFit(i, j)){
                            Pairs.push_back(Pair(i, j));
                        }
                    }
                }
            }
        }else {
			        // so if all color can't drawble
			        // Pair size will be 0
        }
    }
    void showPairs (){
        int s = Pairs.size();	// this function show all the Pairs
        for(int i = 0;i < s; i++){	// of C ... one by one
            Pairs[i].show();
        }
    }
    bool isFit (int x, int y){
        if(((x <= N) && (y <= M))){
            return true;		// this function check whether 
        }else {			// A pair (rect) fit into display (n*m)
            return false;		  
        }
    }
    int getPairsLength (){
        return Pairs.size();	// this function return the number of 
    }				            // Pairs in Pairs vector .
    
    bool isPair (int x, int y){
        if ((x * y) == C){		// this function return true if (x*y) == C
            return true;		// else return false that's it ((x*y) != C)
        }else {
            return false;
        }
    }

};

int main() {
// number of test case;
int T;

// get input of number of test case
cin >> T;

// make an vector of size T;
vector <Testcase> cases;

for(int i = 0; i < T; i++){			// while (i < T) take input of
    int n, m, c;				// (n, m, c) for each testcase
    cin >> n >> m >> c;			// then push them in `cases`
    cases.push_back(Testcase(n, m, c));	// vector
}

for(int i = 0; i < cases.size(); i++){	// for each testcase `tc`
    Testcase tc = cases[i];			// cout the length of Pairs
    cout << tc.getPairsLength() << endl;
}
return 0;

}

One of the best way to handle the wrong ans type problems is that to generate random input by your’s side and then by computer side(By taking random inputs using rand() function in C or C++. It will make you to think more and you will definitely crack the crack the code ASAP…

I am getting the sample output correct but still getting wrong answer. Please help me find my mistake!

#include
using namespace std;
int main()
{ short t,e;
long m;
int k,n,i,j,key,marks;
cin>>t;
int a[10000]={0};
int arr[10001][4]={0};
for(;t>0;t–)
{
cin>>n>>k>>e>>m;
for(j=0;j<n;j++)
{
for(i=0;i<e;i++)
{ if(j==n-1&&i==e-1)
break;
else
{
cin>>arr[j][i];
a[j]+=arr[j][i];
}
}

    }
    
    for(i=1; i<n-1; i++)
    {
        key = a[i];
        j = i-1;
        while(j>=0 && key>a[j])
        {
            a[j+1] = a[j];
            j--;
        }
        a[j+1] = key;
    }
   
    marks=(a[k-1]+1)-a[n-1];
    if(marks<=m)
        if(marks>0)
            cout<<marks;
        else
            cout<<"0";
    else
        cout<<"Impossible ";
    
}
return 0;

}

For the problem- Chef Under Pressure, in the subtask 1, out of 10 tasks, it is showing wrong answer for task 0, rest all are coming correct. I checked for possible corner cases that i might have missed, but couldn’t find anything. Please tell me what is task 0 and its output, so that i can modify my code. Thanks

#include <stdio.h>
#include <stdlib.h>
char a[25];
int top=-1;
void push(char symbol)
{

a[++top]=symbol;

}
char pop()
{

char item;
item=a[top--];
return(item);

}
int precd(char op)
{

int r;
switch(op)
{

case'^':r=3; break;
case'*':
case'/':
case'%':r=2; break;
case'+':
case'-':
case'(':r=0; break;
case'#':r=-1;
 break;
}
return(r);

}
void infix_postfix(char infix[],char postfix[])
{

int i,p=0;
char symbol,item;
push('#');
for(i=0;infix[i]!= '\0';i++)
{

    symbol=infix[i];
    switch(symbol)
    {

        case '(':push(symbol);
        break;
        case ')':item=pop();
         while(item!= '(')
        {

            postfix[p++]=item;
            item=pop();
        }
        break;
        case'+':
            case'-':
            case'*':
            case'/':
            case'%':
            while(precd(a[top])>=precd(symbol));
            {

                item=pop();
                postfix[p++]=item;
            }
            push(symbol);
            break;
            default  : postfix[p++]=symbol;
            break;
    }
}
while(top>0)
{

    item=pop();
    postfix[p++]=item;
}
postfix[p]= '\0';

}
int main()
{
char infix[25],postfix[25];

printf("enter infix expression");
scanf("%s",infix);
infix_postfix(infix,postfix);
printf("\npostfix expression:%s\n\n\t",postfix);
return 0;

}

Please put \n at the time of printing your output…
That’s all
like

printf("%d\n",ans); or cout<<ans<<endl;

2 Likes

#include
using namespace std;
int main()
{
long long int t,n1,n2,k,l;
cin>>t;
for (int i=0;i<t;i++)
{
cin>>n1>>n2;
k=n1;
l=n2;
while(n1 != n2)
{
if(n1>n2)
n1-=n2;
else
n2-=n1;
}
cout<<n1<<" "<<(k*l)/n1<<endl;
}
return 0;
}

can someone help me, trying to submit this code but getting wrong answer.Tried another questions many time still not getting it right.This code displays hcf and lcm of two given numbers.
Is there any role of constraints provided?
or some specific way to output the answer?
i’m a beginner please help.

u dont need to use long long int for it.try this code.it is similar to ur code
#include
using namespace std;

int main()
{
int t,n1,n2;
cin>>t;
while(t–){
cin>>n1>>n2;
int x=n1,y=n2;
while(x!=y){
if(x>y)
x-=y;
else
y-=x;
}
cout<<x<<endl<<n1*n2/x<<endl;
}
return 0;

I FORGOT A } AFTER THE RETURN 0;. ADD THAT ALSO

HOPE IT WAS USEFUL…

Hi, i allways get wrong answer in my codes, even tho im getting the correct answer in various tests.

can someone help me?

this is an example: (Using C)

#include <stdio.h>
#include <stdlib.h>

int main()
{
int maxA,maxB,i,n,A[10000],B[10000],C[10000],D[10000],sum_A=0,sum_B=0;
scanf("%d",&n);
for (i=0;i<n;i++)
{
scanf("%d",&A[i]);
scanf("%d",&B[i]);
C[i]=A[i]-B[i];
D[i]=B[i]-A[i];
//sum_A+=A[i];
//sum_B+=B[i];
maxA=C[i];
maxB=D[i];
}
for (i=1;i<n;i++)
{
if (C[i]>C[(i-1)] && C[i]>maxA)
{
maxA=C[i];
}
else if (C[(i-1)]>C[i] && C[(i-1)]>maxA)
{
maxA=C[(i-1)];
}
if (D[i]>D[(i-1)] && D[i]>maxB)
{
maxB=D[i];

}
    else if (D[(i-1)]>D[i] && D[(i-1)]>maxB)
         {
             maxB=D[(i-1)];
         }

}

if (maxA>maxB)
{
printf("%d %d \n",1,maxA);
}
else
{
printf("%d %d \n",2,maxB);
}
return 0;
}

Contest Code: Practice
Problem code: NUMGAME2
My solution:
#include <bits/stdc++.h>
#define pd(x) printf("%d",x)
#define sld(x) scanf("%ld",&x)
#define plld(x) printf("%lld",x)
#define slld(x) scanf("%lld",&x)
using namespace std;
long long int t;
int isprime (long long int);
int main()
{
long long int n,x,k,q;
slld(t);
while(t–)
{ k=0;
slld(n);
q=n-1;
while(q>=1)
{
if((q==1||isprime(q))&&(n-q)>0)
{
n-=q;
k++;
q=n;
continue;
}
q–;
}
if(k%2==0)
printf(“ALICE\n”);
else
printf(“BOB\n”);
}
return 0;
}
int isprime( long long int N)
{
if(N<2 || (!(N&1) && N!=2))
return 0;
for(int i=3; i*i<=N; i+=2){
if(!(N%i))
return 0;
}
return 1;
}

Plz help to debug my code.

it show wrong ans of matrix transformation

just check out that your program is following the constraints given in the problem.
try to take input just as the given in constraints. This will help you to determine whether your logic is correct or not.
Hope this helps you!!

2 Likes

#include

using namespace std;

int main(){
int t=0,n=0,i=1,j=0,ans=1 ;
cin >> t ;
while(i<=t){
cin >> n ;
for(j=n;j>=1;j–){
ans *=j ;
}
cout << ans << endl;
ans = 1;
i++ ;
}
return 0;
}

THIS IS SHOWING WRONG ANSWER . WHAT IS THE PROBLEM?

Plz give a link to the Q you are solving.

#include

using namespace std ;

int main(){
int i=1,ans,p1,p2,n,lead=0;
cin >> n;
while(i<=n){
cin >> p1 >> p2 ;
lead = lead + p1-p2 ;
i++ ;
}
if(lead>0){
cout << “1” << " " << lead << endl;
}
else{
ans= lead * (-1) ;
cout << “2” << " " << ans << endl;
}
return 0;
}

why is this giving wrong answer?
the url for this problem is https://www.codechef.com/problems/TLG

#include

using namespace std ;

int main(){
int i=1,ans,p1,p2,n,lead=0;
cin >> n;
while(i<=n){
cin >> p1 >> p2 ;
lead = lead + p1-p2 ;
i++ ;
}
if(lead>0){
cout << “1” << " " << lead << endl;
}
else{
ans= lead * (-1) ;
cout << “2” << " " << ans << endl;
}
return 0;
}

why am i getting wrong answer?

@eshan292

I think you misinterpreted the Q.

You have to find the MAXIMUM difference in scores, and the player with the lead wins.

In your case, "lead = lead + p1-p2 " is taking into account the previous lead/lead in previous round, while the example clearly showed this is not the case.

I advice, please give another attempt in understanding the Q, and if you’re still facing problem, look at its solution here