This is a codeChef contest, it means that the answer is never the most straight forward.
The whole codeChef site is about optimization, optimization is about reducing runtime and reducing memory footprint.
In other words, for everything you do, you have to ask yourself why you do something, what is the usage, is it really needed.
ka! your huge array stores result of simple adds and each element is reused only once, you can get rid off that array.
Optimization is also being smart: with sheet of paper and pencil, draw a house of 20*20 and write each room number, you should see a pattern which lead to huge optimization.
-----
Coding style: Learn to indent properly your code, it show its structure and it helps reading and understanding.
#include<stdio.h>
int main()
{
long long int se=0,so=0,fs=0,r,k,t,n,i,j,a[100000][100000];
scanf("%lld",&t);
fs=0;
for(k=0;k<t;k++)
{
scanf("%lld",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
a[i][j]=i+j;
}
}
fs=0;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
while(a[i][j]>0)
{
r=a[i][j]%10;
if(r%2==0)
se=se+r;
else
so=so+r;
a[i][j]=a[i][j]/10;
}
if(se>so)
{
fs=fs+(se-so);se=0;so=0;
}
else
{
fs=fs+(so-se);se=0;so=0;
}
}
}
printf("%lld\n",fs);
}
}
Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[
^]
ultraedit[
^]
[Update]
Quote:
though this was accepted but for large numbers it displayed "Time limit exeeded"
Because you need to find further optimization. This is when this part of the first post comes to play.
Quote:
Optimization is also being smart/clever: with sheet of paper and pencil, draw a house of 20*20 and write each room number, you should see a pattern which lead to huge optimization.