Last Digit TLE ERROR

how can i improve my time for the following code?

import java.io.*;
class LastDigit {
    public static void main(String arg[])throws Exception
	{
		BufferedReader r=new BufferedReader(new InputStreamReader(System.in));
                String ip;
               // System.out.println("enter the no. of test cases");
		int a=Integer.parseInt(r.readLine());
               // sc.nextLine();
                int ipnos [][] = new int[a][2];
           
                for(int i=0;i<a;i++)
                {
                   // System.out.println("enter test cases");
                    ip=r.readLine();
                    
                  String[] arr = ip.split("\\s+");

         
                    ipnos[i][0] = Integer.parseInt(arr[0]);
                    ipnos[i][1] = Integer.parseInt(arr[1]);
                    
                    
                    
                }
               
                for(int i=0 ; i<a; i++)
                {
                    
                    System.out.println(sum1(ipnos[i][0],ipnos[i][1]));
                   
                }
                


        }
  
    
    static int sum1(int a, int b)
    {
       int sum=0,c=0,sumlast=0;
        
       for(int i=a; i<=b;i++){
         
           int m=i;
         while(m!=0){  
                 if( ((m%10)%2) ==0)
                    sum+=2*(m%10);
                else
                    sum+=(m%10);
                 m/=10;
                 
        
         }
         sumlast+= (sum%10);
         sum=0;
    }
       
       return sumlast;

    }
    
    
    
}

problem statement

Problem is in algorithm, not in code.

It runs in O(T*(B-A)), iff the while in sum method is one operation (and it is not).

Max number of operations is 400 bilions!!! You have to find better algorithm…

1 Like