Click here to Skip to main content
15,074,394 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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:

#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".
Posted
Updated 2-Dec-17 2:18am
v8
Comments
Patrice T 2-Dec-17 4:03am
   
Show exact error message and position.
ahmedarifhasan 2-Dec-17 4:20am
   
"Runtime Error: Your code compiled and ran but encountered an error. The most common reasons are using too much memory." Some people suggested using global declaration of array, that doesn't work either, basically i need someway in which i can introduce such a huge array.
Patrice T 2-Dec-17 4:28am
   
Use Improve question to update your question.
So that everyone can pay attention to this information.
ahmedarifhasan 2-Dec-17 4:29am
   
Thank you! :)
Richard MacCutchan 2-Dec-17 4:32am
   
You are trying to use a long long array of 100,000 x 100,000, that is 640 Gigabytes!
ahmedarifhasan 2-Dec-17 4:40am
   
Thank you everyone for helping me, i really appreciate all of you people for taking time out to respond to my query.
Richard MacCutchan 2-Dec-17 4:13am
   
It's not the compiler that shows a SEGV, it's your program. And it is no wonder with all that unintelligible code that you have.
Richard MacCutchan 2-Dec-17 8:59am
   
Please edit your question and get rid of all those extra blank lines, they make it difficult to read.

You don't need arrays, for such a computation. Modify your code to conpute results on the fly.
   
v2
Comments
ahmedarifhasan 2-Dec-17 4:23am
   
can you please elaborate it?
CPallini 2-Dec-17 4:36am
   
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.
ahmedarifhasan 2-Dec-17 4:38am
   
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.
C++
#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
ahmedarifhasan 2-Dec-17 5:51am
   
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".
Patrice T 2-Dec-17 6:06am
   
Use Improve question to update your question.
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.
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
ahmedarifhasan 2-Dec-17 5:59am
   
i have posted a similar solution above, but that exceeds the time limit. :(
Richard MacCutchan 2-Dec-17 6:06am
   
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