15,074,394 members

See more:

Contest Page | CodeChef[^]

This is the error->

"Runtime Error: Time Limit Exceeded."

Some people suggested using global declaration of array, that doesn't work either, basically i need someway in which i can reduce the execution time.

What about use of pointers here?

PS- beginner here and very very weak at pointers.

**What I have tried:**

This is the error->

"Runtime Error: Time Limit Exceeded."

Some people suggested using global declaration of array, that doesn't work either, basically i need someway in which i can reduce the execution time.

What about use of pointers here?

PS- beginner here and very very weak at pointers.

Copy Code

#include<stdio.h> int main() { long long int c, se = 0, so = 0, fs = 0, r , k , t , n , i , j; scanf("%lld",&t); for(k = 0 , fs = 0 ; k< ; t ; k++) { scanf("%lld",&n); for(i = 1; i< = n; i++) { for( j = 1 ; j< = n ; j++) { c = i + j; while(c > 0) { r=c % 10; if(r % 2 == 0) se= se + r; else so= so + r; c= c/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); fs = 0; } } -> though this was accepted but for large numbers it displayed "Time limit exeeded".

Comments

You don't need arrays, for such a computation. Modify your code to conpute results on the fly.

v2

Comments

can you please elaborate it?

given the room number (or the room coordinates {row,col}), its number of diamonds could be computed and summed up. You don't need to store in an array the number of diamonds of each room.

Thank you so much. well, i sent you an invitation on linkedIn, please do accept.

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.

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.

Notepad++ Home[^]

ultraedit[^]

[Update]

Because you need to find further optimization. This is when this part of the first post comes to play.

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.

C++

Copy Code

#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.

v2

Comments

yup i removed the array:

#include<stdio.h>

int main()

{

long long int c, se = 0, so = 0, fs = 0, r , k , t , n , i , j;

scanf("%lld",&t);

for(k = 0 , fs = 0 ; k< ; t ; k++)

{

scanf("%lld",&n);

for(i = 1; i< = n; i++)

{

for( j = 1 ; j< = n ; j++)

{

c = i + j;

while(c > 0)

{

r=c % 10;

if(r % 2 == 0)

se= se + r;

else

so= so + r;

c= c/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);

fs = 0;

}

}

-> though this was accepted but for large numbers it displayed "Time limit

exeeded".

#include<stdio.h>

int main()

{

long long int c, se = 0, so = 0, fs = 0, r , k , t , n , i , j;

scanf("%lld",&t);

for(k = 0 , fs = 0 ; k< ; t ; k++)

{

scanf("%lld",&n);

for(i = 1; i< = n; i++)

{

for( j = 1 ; j< = n ; j++)

{

c = i + j;

while(c > 0)

{

r=c % 10;

if(r % 2 == 0)

se= se + r;

else

so= so + r;

c= c/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);

fs = 0;

}

}

-> though this was accepted but for large numbers it displayed "Time limit

exeeded".

Use **Improve question** to update your question.

So that everyone can pay attention to this information.

So that everyone can pay attention to this information.

Try the KISS method, i.e. Keep It Simple, Stupid.

Start with the basic problem N rows and N columns gives the total number of rooms.

From that you can test larger values of N.

Once you are happy with that you can add the code to get different values of T and loop through multiple values of N. The print statement in the inner loop is for debug purposes only and can be removed when the code is working successfully.

Start with the basic problem N rows and N columns gives the total number of rooms.

Copy Code

N = 3 // simple test value totalDiamonds = 0 FOR row = 1 to N BEGIN FOR column = 1 to N BEGIN roomNumber = row + column evenDigits = // calculate the sum of even digits oddDigits = // calculate the sum of odd digits diamonds = ABS(evenDigits - oddDigits) print roomNumber ": " diamonds totalDiamonds += diamonds END END print "Total number of diamonds: " totalDiamonds

From that you can test larger values of N.

Once you are happy with that you can add the code to get different values of T and loop through multiple values of N. The print statement in the inner loop is for debug purposes only and can be removed when the code is working successfully.

Comments

i have posted a similar solution above, but that exceeds the time limit. :(

My code is just a starting point to solve the basics. Your task is to find ways of improving it so it runs as fast as possible. I have never looked at CodeChef before so I don't really have anything more to suggest.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject,
20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8
+1 (416) 849-8900

Improve questionto update your question.So that everyone can pay attention to this information.