Need explanation of Datatype why here it is used.

#include <stdio.h>
int main()
{
int t;
scanf(“%d”, &t);
while (t–)
{
int n, A, B;
scanf(“%d%d%d”, &n, &A, &B);
char arr[100][101];
for (int i = 0; i < n; i++)
{
scanf(“%s”, arr[i]);
}
char gw[7] = “EQUINOX”;
long count = 0;
for (int i = 0; i < n; i++)
{

        for (int j = 0; j < 7; j++)
        {
            if (arr[i][0] == gw[j])
            {
                count++;
                
            }
        }
    }
    long sar = count * A, anu = (n - count) * B;
    if (sar > anu)
    {
        printf("SARTHAK\n");
    }
    else if (anu > sar)
    {
        printf("ANURADHA\n");
    }
    else
    {
        printf("DRAW\n");
    }
}

}

This is the code of this (CodeChef: Practical coding for everyone) question.
In this code if i change the (line no. 16) code “long” to “int” then after submitting it is showing wrong answer otherwise correct.
The number of count never exceed “N” or “100”. Then, Why i need to give datatype of “count” as “long”. .

long sar = count * A

count <= 10²
A <= 10⁹
count * A <= 10¹¹

If both count and A are ints, their product will be an int, which will cause an Integer Overflow for some Test cases. That int will be stored inside a long, but only after the Overflow happened. Meaning either count or A has to be a long.

2 Likes