Recursive Digit Sum

  • + 2 comments
    [deleted]
    • + 5 comments

      Here's the C++ Code, All test cases passing

      int num1(long long int z)
      {
          if(z<=9)
          {
              return z;
          }
          long long int sum=0;
          long long int m=0;
      
          while(z!=0)
          {
              
              m=z%10;
              sum=sum+m;
              z=z/10;
      
      
          }
          return num1(sum);
      
      
      }
      
      // Complete the superDigit function below.
      int superDigit(string n, int k) {
      long long int sum=0;
      long long int m;
         for(int i=0; i<n.size(); i++){
              sum += n[i] - '0';
          }
      
          return num1(sum*k);
      
      }
      
      • + 0 comments

        Can you explain why doesn't it overflow in test case 9?

      • + 0 comments

        hey when the range of n is so large then how did your code got get all test cases passed .

      • + 0 comments

        I avoided % and / and passed all tests in cpp.

      • + 4 comments

        can u explain if the n is string how u are converting it in int

        • + 0 comments

          use function stoi( )

        • + 0 comments

          using ASCII, Since all numerical characters will be in series in ASCII table. eg. '9' - '0' = 9 and so on

        • + 0 comments

          using ASCII value like this : (int)s[i]-'0'

      • + 0 comments

        Can you please explain why you called num1 function by passing sum*k