Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
(Reference: https://leetcode.com/problems/add-digits/)
Solution:
Let's take a couple of example to check the input and expected output of the given problem.
Example 1:
Input: 267
Step 1: 2+6+7 = 15
Step 2: 1+5 = 6 (Expected output as this number has single digit)
Example 2:
Input: 7713
Step 1: 7+7+1+4 = 19
Step 2: 1+ 9 = 10
Step 3: 1+0 = 1 (Expected output as this number has only 1 digit)
By looking at the above examples the first solution that comes to mind is that we can take the input number and find the sum of individual digits by using recursion (or iteration). If the result is not a single digit code we ill process the result again and keep doing it until it returns a single digits. This methods is correct and works for all the valid inputs. The code for this algorithm can be writtes as follows:
1 2 3 4 5 6 7 8 9 10 11 12 | public int addDigits(int num) { while(num/10>0){ num = sumDigits(num); } return num; } public int sumDigits(int n){ if(n==0) return 0; return (n%10) + sumDigits(n/10); } |
Can we find anything better than the above solution. Let's take a couple of more example and see if we can deduce some pattern for the result:
Example 3:
Input: 10
Step 1: 1+0 = 1 (Expected output)
Example 4:
Input: 11
Step 1: 1+1 = 2 (Expected output)
Example 5:
Input: 12
Step 1: 1+2 = 3 (Expected output)
Example 6:
Input: 18
Step 1: 1+8 = 9 (Expected output)
Arguments: In mathematics we have learnt that any number that is divisible by 9, the sum of the digits in the number is also divisible by 9. Also, here we know that the result of the problem is an integer lying in the range [0,9] .
From the above arguments and samples, we can see that the result depends on the divisibility of a number by 9. The code can be written as follows:
1 2 3 4 5 6 7 8 | public int addDigits(int num) { if(num<10) return num; else if(num%9 ==0) return 9; else return num%9; } |
Please do let me know your comments about this blog post.
Thanks. Happy Coding :)
No comments:
Post a Comment