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.
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.
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 →
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.
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))