Recursive Digit Sum problem hackerRank

 We define super digit of an integer  using the following rules:


Given an integer, we need to find the super digit of the integer.


If  has only  digit, then its super digit is .

Otherwise, the super digit of  is equal to the super digit of the sum of the digits of .

For example, the super digit of  will be calculated as:


super_digit(9875)    9+8+7+5 = 29 

super_digit(29) 2 + 9 = 11

super_digit(11) 1 + 1 = 2

super_digit(2) = 2  

Example



The number  is created by concatenating the string   times so the initial .


    superDigit(p) = superDigit(9875987598759875)

                  9+8+7+5+9+8+7+5+9+8+7+5+9+8+7+5 = 116

    superDigit(p) = superDigit(116)

                  1+1+6 = 8

    superDigit(p) = superDigit(8)

All of the digits of  sum to . The digits of  sum to .  is only one digit, so it is the super digit.


Function Description


Complete the function superDigit in the editor below. It must return the calculated super digit as an integer.


superDigit has the following parameter(s):


string n: a string representation of an integer

int k: the times to concatenate  to make 

Returns


int: the super digit of  repeated  times

Input Format


The first line contains two space separated integers


Solution :



import java.io.*;

class Result {

    /*
     * Complete the 'superDigit' function below.
     *
     * The function is expected to return an INTEGER.
     * The function accepts the following parameters:
     *  1. STRING n
     *  2. INTEGER k
     */

    public static int superDigit(String n, int k) {
        long sum = 0;

        // Compute the sum of digits of n
        for (char digit : n.toCharArray()) {
            sum += digit - '0';  // Convert char to integer
        }

        // Multiply sum by k (since the number is repeated k times)
        sum *= k;

        // Compute the super digit using recursion
        return superDigitHelper(sum);
    }

    private static int superDigitHelper(long num) {
        if (num < 10) return (int) num; // Base case: if single-digit, return it

        long sum = 0;
        while (num > 0) {
            sum += num % 10;  // Extract last digit and add to sum
            num /= 10;         // Remove last digit
        }

        return superDigitHelper(sum); // Recur until a single-digit number is obtained
    }
}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        String[] firstMultipleInput = bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");

        String n = firstMultipleInput[0];
        int k = Integer.parseInt(firstMultipleInput[1]);

        int result = Result.superDigit(n, k);

        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();

        bufferedReader.close();
        bufferedWriter.close();
    }
}




Comments

Popular posts from this blog

Two Sum II - Input Array Is Sorted

Comparable Vs. Comparator in Java

Increasing Triplet Subsequence