A magical number is obtained from a positive number by adding its digits repeatedly until we obtain one digit.
Example 1:
Input: N = 39 Output: 3 Explanation: magicNumber(39) = magicNumber(3 + 9) = magicNumber(12) = magicNumber(1 + 2) = 3
Example 2:
Input: N = 928435 Output: 4 Explanation: 9 + 2 + 8 + 4 + 3 + 5 = 31 => 3 + 1 = 4
Walkthroughs of the solution. Try the steps first, then watch to compare approaches.
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check