Given a non-negative integer num, keep replacing it with the sum of its digits until only one digit remains, and return that digit. Follow up: can you do it in O(1) time without any loop or recursion?
Given a non-negative integer num, keep replacing it with the sum of its digits until only one digit remains, and return that digit. Follow up: can you do it in O(1) time without any loop or recursion?
Example 1:
Input: {"num":38}
Output: 2
Constraints:
0≤num≤231−1
Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result
Run code to see results.
Input
arr ={"num":38}
The slow way: keep adding the digits of 38 until one digit is left. Watch what stays the same.