We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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.
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));
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Join us
Create a HackerRank account
Be part of a 26 million-strong community of developers
Please signup or login in order to view this challenge
Recursive Digit Sum
You are viewing a single comment's thread. Return to all comments →
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.
superDigit(repeat k times of number_n) = superDigit( k * superDigit(number_n))