Recursive Digit Sum

  • + 1 comment

    If someone out there is having any issues with some of the test cases even though their solution is correct you might want to look at the constraints, using a integer then converting that to string to store your sum is not the move, it will work but not for some of the test cases, it might be worth making your sum append to a string or list and see it that pass some of the test cases.

    • + 1 comment

      For mine, I started out by making the string super long. Instead, I should have multiplied the sum by "k". Making the string super long would take way too long, which was why it didn't work.

      • + 0 comments

        superDigit(repeat k times of number_n) = superDigit( k * superDigit(number_n))

        public static int superDigitHelper(String n) {
            int sum = 0;
            for(int i=0; i<n.length();i++){
              sum += Character.getNumericValue(n.charAt(i));
            }
            if(sum>=10){
                sum = superDigitHelper(String.valueOf(sum));
            }
            return sum;
        }
        public static int superDigit(String n, int k) {
            // Write your code here
            int superDigitN = superDigitHelper(n);
            int p = superDigitN*k;
            return superDigitHelper(String.valueOf(p));
        }