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.
I noticed that some solutions provided here do not work for the simple case when "n" only has one digit but "k" bigger than one, for example when n=1 and k=4 (example input: "1 4") the result should logically be "4".
(Note: this test case is not one of the provided test cases).
publicstaticintsuperDigit(Stringn,intk){// Write your code hereif(n.length()==1&&k==1){returnInteger.valueOf(n);}else{String[]digits=n.split("");longsum=0;for(Stringdigit:digits){sum+=Integer.valueOf(digit);}sum=sum*k;returnsuperDigit(String.valueOf(sum),1);}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Recursive Digit Sum
You are viewing a single comment's thread. Return to all comments →
I noticed that some solutions provided here do not work for the simple case when "n" only has one digit but "k" bigger than one, for example when n=1 and k=4 (example input: "1 4") the result should logically be "4". (Note: this test case is not one of the provided test cases).