why did this PROSUM solution not get accepted?

this solution for PROSUM gave a wrong answer… i dont understand why, while the same solution with the data type changed
long long int got accepted. Why did the first solution not get accepted?


#include<stdio.h>
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {   unsigned long int ans,n,i,count=0,c=0;
        scanf("%lu",&n);
        unsigned long int a;
        for(i=0; i<n; i++)
        {   scanf("%lu",&a);
            if(a!=1 && a!=0) {count++;}
            if(a==2) {c++;}
        }
        ans=((count*(count-1))/2)-((c*(c-1))/2);
        printf("%lu\n",ans);
        }
    return 0;
}

this below code got accepted


 #include<stdio.h>
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {   long long int ans,n,i,count=0,c=0;
        scanf("%lld",&n);
        long long int a;
        for(i=0; i<n; i++)
        {   scanf("%lld",&a);
            if(a!=1 && a!=0) {count++;}
            if(a==2) {c++;}
        }
        ans=((count*(count-1))/2)-((c*(c-1))/2);
        printf("%lld\n",ans);
        }
    return 0;
} 
1 Like

#include<stdio.h>

int main()

{

printf("%d %d",sizeof(unsigned long int),sizeof(long long int));
return 0;

}

On 32 bit machine !

In the first solution, count and c was declared ‘unsigned long’ so it gave wrong answer because of the overflow from the 32 bit integer range.
And in the second case there was no overflow as count and c both were declared as ‘long long int’ as the range is: -2^63+1 to +2^63-1

3 Likes