![]() |
VOOZH | about |
Given a large number num in the form of string with length as N, the task is to find its digital root.
The digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root. If the resulting value contains two or more digits, those digits are summed and the process is repeated. This is continued as long as necessary to obtain a single digit.
Examples:
Input: num = 675987890789756545689070986776987
Output: 5
Explanation:
Sum of individual digit of the above number = 212
Sum of individual digit of 212 = 5
So the Digital root is 5Input: num = 876598758938317432685778263
Output: 2
Explanation:
Sum of individual digit of the above number = 155
Sum of individual digit of 155 = 11
Sum of individual digit of 11 = 2
So the Digital root is 2
Approach:
Follow the below steps to solve the problem:
Below is the implementation of the above approach:
5
Time Complexity: O(N), which can be computed as follows:
Auxiliary Space: O(1), since the sum stored in the string will not exceed 3 digits for N ? 105