PLZ HELP..HOW TO REDUCE TIME COMPLEXITY ??

#include <stdio.h>
//#include <conio.h>
int main()
{
 //clrscr();
 int t;
 long m,n,num,sum=0,lsum=0,i;
 scanf("%d",&t);
 while(t--)
 {
  scanf("%ld%ld",&m,&n);
  i=m;
  while(i<=n)
  {
   //num=i;
   while(i!=0)
   {
     num=i%10;
     i=i/10;
     if(num%2==0)
      sum=sum+2*num;
     else
      sum=sum+num;
   }
   lsum=lsum+(sum%10);
   i++;
  }
 }
 printf("\n%ld",lsum);
 return 0;
 }

can you state the question for which you have coded this.

instead of i=i/10 use 1/=10;
sum+=(num<<1);
and a few more like this,but it won’t really help much

give the link of the file you have problem with or the question so we can try too

while(t–), there is no condition declared within while loop

it’s c so when t-- will be 0 it’ll exit

1 Like

Can you state the problem you are trying to solve? Also mention what is your current approach. Seems like a poorly framed question (?)

this is the question…http://www.codechef.com/problems/LASTDIG

You are dividing the i: i/=10. This means that i is always set back to 1 at the end of the loop. You should use a temporary variable for the dividing. Like this:

 while(i<=n){
     int temp=i;
     while(temp!=0){
         num=temp%10;
         temp/=10;
     }
     i++;
 }

P.S.: There are many other errors in your code in solving the stated problem.But they are not connected with the infinite looping.

1 Like