# Why do I get Wrong Answer?

**URL:** <https://discuss.codechef.com/t/why-do-i-get-wrong-answer/1786>\
**Category:** faq\
**Tags:** faq, wa\
**Created:** [March 22, 2013, 4:31pm UTC](https://discuss.codechef.com/t/why-do-i-get-wrong-answer/1786 "2013-03-22T16:31:40Z")\
**Posts on this page:** 20\
**Page:** 3

<div class="post-metadata">

**Author:** ![char\_star](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@char\_star](https://discuss.codechef.com/u/char_star)\
**Post date:** [April 24, 2016, 11:03pm UTC](https://discuss.codechef.com/t/why-do-i-get-wrong-answer/1786/41 "2016-04-24T23:03:12Z")

</div>

#include   
#include   
using namespace std;

```
int main() {
	
int n[10];

for(int a = 0; a<10; a++)
{
	cin>>n[a];
}
for(int x = 0; x <10; x++){
	if (n[x]==42)
	break;
	else	{
		cout<<n[x]<<"\n";
	}
}
	return 0;
}

```

Why do I get wrong answer? However the program runs perfectly on local machine but on codechef it says wrong answer! Please help.

---

<div class="post-metadata">

**Author:** ![shivang\_py](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@shivang\_py](https://discuss.codechef.com/u/shivang_py)\
**Post date:** [April 29, 2016, 12:12pm UTC](https://discuss.codechef.com/t/why-do-i-get-wrong-answer/1786/42 "2016-04-29T12:12:16Z")

</div>

This is my code in python3 for beginner COLOUR problem and it gives correct answer for all sample input. But still the result is “Wrong answer”?

```
t=int(input()) #no. of test cases
while t>0:
    n=int(input()) #no of rooms in chef's house
    s=str(input()) #colour config
    mnr=0
    cr=0
    cb=0
    cg=0
    for st in s:
        if st=="R" or st=="B" or st=="G":
            if st=="R":
                    cr+=1
            elif st=="B":
                    cb+=1
            elif st=="G":
                    cg+=1
    if cr==cb and cr==cg:
        mnr=cr*2
    else:
        mnr=n-max(cr,cb,cr)
    print(mnr)
    t-=1
```

---

<div class="post-metadata">

**Author:** ![raygmurphy](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@raygmurphy](https://discuss.codechef.com/u/raygmurphy)\
**Post date:** [April 30, 2016, 3:00pm UTC](https://discuss.codechef.com/t/why-do-i-get-wrong-answer/1786/43 "2016-04-30T15:00:52Z")

</div>

I’ve tried to find answer to your problem. Test your program by some custom test cases and check the output, the results show why your program isn’t passing all test cases. I hope you can fix them by yourself. For more reference seek help from [custom essay writing service](http://american-writers.org/).

---

<div class="post-metadata">

**Author:** ![meight\_888](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@meight\_888](https://discuss.codechef.com/u/meight_888)\
**Post date:** [May 1, 2016, 4:48pm UTC](https://discuss.codechef.com/t/why-do-i-get-wrong-answer/1786/44 "2016-05-01T16:48:01Z")

</div>

thanks you friend

---

<div class="post-metadata">

**Author:** ![strangecoder\_1](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@strangecoder\_1](https://discuss.codechef.com/u/strangecoder_1)\
**Post date:** [May 7, 2016, 12:04am UTC](https://discuss.codechef.com/t/why-do-i-get-wrong-answer/1786/45 "2016-05-07T00:04:10Z")

</div>

#include  
using namespace std;  
int main()  
{  
string s;  
int n,k;

```
cin>>k;
for(int j=0;j<k;j++)
{

cin>>s;
n=s.size();
for(int i=n;i>=0;i--)
    cout<<s[i];
cout<<"\n";
}

```

}

I’ve tested the code but it gives me a wrong answer can anyone tell me why??

---

<div class="post-metadata">

**Author:** ![keertika32](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@keertika32](https://discuss.codechef.com/u/keertika32)\
**Post date:** [May 11, 2016, 5:24pm UTC](https://discuss.codechef.com/t/why-do-i-get-wrong-answer/1786/46 "2016-05-11T17:24:40Z")

</div>

#include   
#include   
using namespace std;

int main() {

long long int t,i,n,w,c;

```
// your code goes here

cin>>t;

while(t--)

{c=0;
      cin>>n;

   char a[n],b[n];

  long long int w[n+1];

    
    for(i=0;i<n;i++)

   { cin>>a[i];
   if(a[i]<65||a[i]>90)
   a[i]='_';
   }
  
    for(i=0;i<n;i++)

    { cin>>b[i];
   if(a[i]<65||a[i]>90)
   a[i]='_';
   }

    for(i=0;i<(n+1);i++)

    cin>>w[i];
   

    for(i=0;i<n;i++)

    {

    if(a[i]==b[i])

    c++;

    }
    cout<<w[c]<<endl;

```

}  
return 0;  
}

Why am I getting wrong answer?

---

<div class="post-metadata">

**Author:** ![arnav\_roy](https://discuss.codechef.com/user_avatar/discuss.codechef.com/arnav_roy/32/24644_2.png) [@arnav\_roy](https://discuss.codechef.com/u/arnav_roy)\
**Post date:** [June 12, 2016, 3:04pm UTC](https://discuss.codechef.com/t/why-do-i-get-wrong-answer/1786/47 "2016-06-12T15:04:17Z")

</div>

> **[HS08TEST Problem - CodeChef](https://www.codechef.com/problems/HS08TEST)**
>
> Practice your programming skills with easy level problem on Basic Programming.

Why am I getting a wrong answer?

#include  
#include  
using namespace std;  
int main()  
{  
int a ;  
double b;  
cin \>\> a\>\> b;  
if ((0\< a\<=2000) &&(0\<=b\<=2000) && (a \<= b ))  
{  
if( a % 5 == 0 )

```
	cout <<fixed << setprecision(2)<< b - a - 0.50 << "\n" ;

else 
     cout << fixed << setprecision(2) << b << "\n";

```

}  
else  
cout \<\< fixed \<\< setprecision(2) \<\< b\<\< “\n”;  
}

---

<div class="post-metadata">

**Author:** ![runjhun123](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@runjhun123](https://discuss.codechef.com/u/runjhun123)\
**Post date:** [July 5, 2016, 11:05am UTC](https://discuss.codechef.com/t/why-do-i-get-wrong-answer/1786/48 "2016-07-05T11:05:02Z")

</div>

why m i getting wrong answer in this …question code CLEANUP?

#include\<stdio.h\>  
main()  
{  
int T,t,n,m,i,k,N[1000],j,flag,chef[1000],asst[1000],x;  
scanf("%d",&t);  
for (T=0;T\<t;T++)  
{  
scanf("%d",&n);  
scanf("%d",&m);  
for(i=0;i\<=n-1;i++)  
{  
N[i]=1;  
}

```
   for(i=0;i<=m-1;i++)
   {
         scanf("%d",&x);
         N[x-1]=0;
         
       }
   flag=0;
   k=0;
   j=0;
   for(i=0;i<=n-1;i++)
   {
        if(N[i]==1)
        {
           if(flag==0)
           {
           chef[k]=i+1;
           k++;
           flag=1;        
           }
           else
           {
            asst[j]=i+1;
           j++;
           flag=0;  
          }
        }
   }
    for(i=0;i<=k-1;i++)
     {
        printf("%d",chef[i]);
     }
     printf("\n");
    for(i=0;i<=j-1;i++)
     {
        printf("%d",asst[i]);
     }
     printf("\n");
  }
   return 0;

```

}

---

<div class="post-metadata">

**Author:** ![tiwari9](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@tiwari9](https://discuss.codechef.com/u/tiwari9)\
**Post date:** [July 6, 2016, 2:13am UTC](https://discuss.codechef.com/t/why-do-i-get-wrong-answer/1786/49 "2016-07-06T02:13:28Z")

</div>

#include\<stdio.h\>  
#include\<stdlib.h\>  
int main()  
{  
long long t,n,i,j,m,s,x,o,a,b;  
scanf("%lld",&t);  
while(t–)  
{  
o=0;  
scanf("%lld",&n);  
char A[n];  
long long B[n];  
scanf("%s",A);  
for(i=0;i\<n;i++)  
scanf("%lld",&B[i]);  
for(i=0;i\<n;i++)  
{  
m=100000000;  
if(A[i]==48)  
{  
for(j=0;j\<n;j++)  
{  
if(A[j]==49)  
{  
s=abs(B[j]-B[i]);  
if(m\>s)  
{  
m=s;  
x=j;  
}  
}  
}  
o+=abs(B[x]-B[i]);  
A[i]=49;  
}  
}  
printf("%lld\n",o);  
}  
return 0;  
}  
why i get WA

---

<div class="post-metadata">

**Author:** ![ekjot\_kaur](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@ekjot\_kaur](https://discuss.codechef.com/u/ekjot_kaur)\
**Post date:** [August 4, 2016, 9:48pm UTC](https://discuss.codechef.com/t/why-do-i-get-wrong-answer/1786/50 "2016-08-04T21:48:55Z")

</div>

#include\<stdio.h\>  
int main(){  
int t,n;  
int a[200];

scanf("%d",&t);  
while(t–){  
scanf("%d",&n);  
int temp=0;  
int i=2;  
a[0]=1;  
int m=1;  
int x;  
while(i\<=n){  
int index=0;  
while(index\<m){  
x=a[index]\*i+temp;  
a[index]=x%10;  
temp=x/10;  
index++;  
}  
while(temp!=0){  
x=temp%10;  
a[index]=x;  
temp=temp/10;  
m++;  
index++;  
}  
i++;

}  
int k;  
for( k=m-1;k\>=0;k–)  
printf("%d",a[k]);

}

}

Why my answers is wrong ??

---

<div class="post-metadata">

**Author:** ![krishnakk789](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@krishnakk789](https://discuss.codechef.com/u/krishnakk789)\
**Post date:** [August 21, 2016, 10:43am UTC](https://discuss.codechef.com/t/why-do-i-get-wrong-answer/1786/51 "2016-08-21T10:43:07Z")

</div>

#include  
using namespace std;

int main()  
{  
int x;  
float y;  
cin\>\>x\>\>y;  
if(x%5!=0 || x\>=y)  
{  
cout\<\<y\<\<endl;  
}

```
else if(x%5==0)
{
    cout<<y-x-0.5<<endl;
}

return 0;

```

}  
why am i getting wrong answer?

---

<div class="post-metadata">

**Author:** ![kama7](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@kama7](https://discuss.codechef.com/u/kama7)\
**Post date:** [August 21, 2016, 12:18pm UTC](https://discuss.codechef.com/t/why-do-i-get-wrong-answer/1786/52 "2016-08-21T12:18:02Z")

</div>

Check this code for this problem - [Ice Cream Problem](https://www.codechef.com/CCWR2016/problems/CCWR01)  
Working perfectly for sample example - [My Solution](https://www.codechef.com/viewsolution/11216636)

---

<div class="post-metadata">

**Author:** ![champrg](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@champrg](https://discuss.codechef.com/u/champrg)\
**Post date:** [September 2, 2016, 12:50pm UTC](https://discuss.codechef.com/t/why-do-i-get-wrong-answer/1786/53 "2016-09-02T12:50:30Z")

</div>

why i get wrong answer

---

<div class="post-metadata">

**Author:** ![shuvajit](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@shuvajit](https://discuss.codechef.com/u/shuvajit)\
**Post date:** [September 7, 2016, 1:37am UTC](https://discuss.codechef.com/t/why-do-i-get-wrong-answer/1786/54 "2016-09-07T01:37:29Z")

</div>

#include\<stdio.h\>  
main()  
{  
int a[100];  
int cnt=0,i=0,j=0;  
printf(“enter no of digit”);  
scanf("%d",&j);  
printf(“enter”);  
for(i=0;i\<=j;i++)  
{  
scanf("%d",&a[i]);  
}  
for(i=0;i\<=j;i++)  
{  
if(a[i]!=42)  
printf("%d",a[i]);  
else  
break;  
}  
}  
what is wrong with this code???

---

<div class="post-metadata">

**Author:** ![vddesai](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@vddesai](https://discuss.codechef.com/u/vddesai)\
**Post date:** [September 13, 2016, 7:52pm UTC](https://discuss.codechef.com/t/why-do-i-get-wrong-answer/1786/55 "2016-09-13T19:52:31Z")

</div>

#include \<stdio.h\>  
#include \<string.h\>

int main(void)  
{  
int T;  
int win = 0;  
int lose = 0;  
char S[100];

```
do 
{
    scanf("%d",&T);
}
while ( T < 0 || T > 1000);

for(int j = 0; j < T;j++)
{
scanf("%s",S);

for(int i = 0; i < strlen(S); i++)
{
    if(S[i] != '1')
        ++lose;
    else
        ++win;
}

if (win > lose)
    printf("WIN\n");
else
    printf("LOSE\n");

}
 return 0;   
}

```

why WA ?

---

<div class="post-metadata">

**Author:** ![upreti\_bhawna](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@upreti\_bhawna](https://discuss.codechef.com/u/upreti_bhawna)\
**Post date:** [September 22, 2016, 9:30am UTC](https://discuss.codechef.com/t/why-do-i-get-wrong-answer/1786/56 "2016-09-22T09:30:04Z")

</div>

import java.util.\*;  
class cd2  
{  
public static void main(String ar[])  
{  
Scanner s=new Scanner(System.in);  
Long T,D;  
T=s.nextLong();  
while(T–!=0)  
{  
D=s.nextLong();  
Long w,z;  
w=(long)0;  
z=(long)0;  
while(D\>0)  
{  
if(D%10==1)  
w++;  
else  
z++;  
D=D/10;  
}  
if((w==1)||(z==1))  
System.out.println(“Yes”);  
else  
System.out.println(“No”);  
}  
}  
}

It’s giving wrong answer.

---

<div class="post-metadata">

**Author:** ![kiamottullah](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@kiamottullah](https://discuss.codechef.com/u/kiamottullah)\
**Post date:** [October 20, 2016, 9:40am UTC](https://discuss.codechef.com/t/why-do-i-get-wrong-answer/1786/57 "2016-10-20T09:40:39Z")

</div>

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;

```

}

---

<div class="post-metadata">

**Author:** ![bansal1232](https://discuss.codechef.com/user_avatar/discuss.codechef.com/bansal1232/32/28353_2.png) [@bansal1232](https://discuss.codechef.com/u/bansal1232)\
**Post date:** [October 20, 2016, 10:37am UTC](https://discuss.codechef.com/t/why-do-i-get-wrong-answer/1786/58 "2016-10-20T10:37:39Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![gaurav\_jain013](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@gaurav\_jain013](https://discuss.codechef.com/u/gaurav_jain013)\
**Post date:** [October 23, 2016, 3:06pm UTC](https://discuss.codechef.com/t/why-do-i-get-wrong-answer/1786/59 "2016-10-23T15:06:49Z")

</div>

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;

```

}

---

<div class="post-metadata">

**Author:** ![vishal\_703](https://www.codechef.com/sites/all/themes/abessive/images/user_default_thumb.jpg) [@vishal\_703](https://discuss.codechef.com/u/vishal_703)\
**Post date:** [October 29, 2016, 3:48pm UTC](https://discuss.codechef.com/t/why-do-i-get-wrong-answer/1786/60 "2016-10-29T15:48:01Z")

</div>

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

[Previous page](https://discuss.codechef.com/t/why-do-i-get-wrong-answer/1786.md?page=2)

[Next page](https://discuss.codechef.com/t/why-do-i-get-wrong-answer/1786.md?page=4)
