My code looks complicated compared to others. But it was working on my console, but gave wrong answer for codechef judge.
It became complicated because
- I thought scanf will not read variable number of integers on single line and hence read things character by character.
- I thought even though long long int will store one array element properly, it will not store sum of two very large numbers a[i]+a[j]. Hence used long long unsigned int. Now I am realising few guys with only int also got correct answer
- Since I used long long unsigned int , abs does not have prototype for unsigned int, so manually created absolute.
I want to be sure if array elements were actually provided on single line separated by single space. Otherwise I dont know what could have gone wrong. If someone takes effort to read this code and suggest what could have gone wrong, it will really help.
#include<stdio.h>
#include<stdlib.h>
long long unsigned int notest,array[1002],n,c,d,swap,k,min1,min2,minval,count,absval,e;
char temp;
main()
{
scanf("%llu",¬est);
while (notest–)
{
scanf("%llu %llu",&n,&k);
min2 = 0;
minval = 0;
d = 1;
e = 0;
while(1)
{
temp = getchar();
if ((d==1) && (temp==10))
continue;
if ((temp== 32) || (temp == '\t'))
if (e!=0)
{
array[d++] = e;
e = 0;
continue;
}
else
continue;
if (temp== 10)
break;
if ((temp >=48) && (temp <=57))
e = e* 10 + temp - 48;
}
array[d] = e;
count = 0;
minval = 1000000001;
for (c=1;c<=n;c++)
for (d=c+1;d<=n;d++)
{
if ((array[c]+array[d]) > k)
absval = array[c]+array[d] - k;
else
absval = k - (array[c]+array[d]);
if (absval < minval)
{
minval = absval;
count = 1;
}
else if (absval == minval)
{
count++ ;
}
}
printf("%llu %llu\n",minval,count);
}
return 0;
}