Help me figure out the logical issue in this code

, ,

Problem Statement: CodeChef: Practical coding for everyone

My Code:

#include <stdio.h>
int main() 
{
	int t;
    scanf("%d", &t);
    int sum1=0;
    int sum2=0;
	
	while(t--)
	{
	    int A[10];
	    for(int i = 0; i < 10; i++)
	    {
	        scanf("%d", &A[i]);
	        if(i%2==0)
	        {
	            sum2=sum2+A[i];
	        }
	        else
	        {
	            sum1=sum1+A[i];
	        }
	    }
	    if(sum1==sum2)
	    {
	        printf("0\n");
	    }
	    else if(sum1>sum2)
	    {
	        printf("1\n");
	    }
	    else
	    {
	        printf("2\n");
	    }
	    
    }
}

Expected vs what I’m getting:

Hope I’m not doing anything silly :sweat_smile:
Thanks

@swastimishra01
The problem statement has assigned team scores on 1-base indexing, (1,2,3…,10),whereas we are using a zero-base indexing, (0,1,2…9),in the given code.

So, the odd based ones become even and vice-versa.

In the code, if we initialized the loop iteration like;

for(i=1;i<=10;i++)
{

}

Then we could follow exactly as the problem statement has stated.

Also, you could simple swap variables, sum1 and sum2 and get the desired output.

Here is my code for reference.

// Update your code below to solve the problem

#include <stdio.h>
int main() 
{
	int t;
    scanf("%d", &t);
	
	while(t--)
	{
	    int A[10],d=0;
	    for(int i = 0; i < 10; i++)
	    {
	        scanf("%d", &A[i]);
	        if((i%2)==0)
	        {
	            if(A[i]==1)
	            {
	                d++;
	            }
	            
	        }
	        else
	        {
	            if(A[i]==1)
	            {
	                d--;
	            }
	        }
	    }
	    if(d>0)
	    {
	        printf("1\n");
	    }
	    else if(d==0)
	    {
	        printf("0\n");
	    }
	    else
	    {
	        printf("2\n");
	    }
    }
}

1 Like