0

Please help me find the time-complexity of below recursive algo. I have guessed for first two return statements.Please correct me if I am wrong. But I can't find time-complexity of third return statement because it will loop till we get a factorial of NUM contining 0 and 7.

    public class Check
   {

   static long NUM = 11;
   public static void main(String[] args)
   {

       System.out.print(find007(NUM));
   }

     static long find007(long n){

       if(is007(n))  //O(1)
         return n;

       if(n+NUM<n)   //O(1)
         return 0;

       return find007(n+NUM);  //O(?)
     }
     static boolean is007(long n){
        while(n!=0 && (n%10==0 || n%10==7))  //O(log n)
         n=n/10;

       return n==0;
     }
}

Pseudo-code:

static long find007(long n){

 check if number or its multiple consists of only 0 or 7.
if yes, return the answer/number
 return n;

//if not, keep checking for the other multiples of NUM
i.e n+NUM (here NUM=11, so it will keep adding 11 to itself till 77)
   return find007(n+NUM);
}

 static boolean is007(long n){

   continue till n!=0 and  n%10==0 OR n%10==7
        n=n/10;

     return true if n==0;         

         }
Raphael
  • 73,212
  • 30
  • 182
  • 400
Ankita
  • 1
  • 2

0 Answers0