Skip to content

Commit d2cdfd6

Browse files
authored
Merge pull request #1542 from agarwalhimanshugaya/dsa
add question no 258
2 parents 2582440 + 66a90dd commit d2cdfd6

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
---
2+
id: add-digits
3+
title: Add Digits
4+
sidebar_label: 0258-Add-digits
5+
tags: ['Math', 'Simulation', 'NumberTheory' ]
6+
description: "Given an integer num, repeatedly add all its digits until the result has only one digit, and return it."
7+
---
8+
9+
## Problem
10+
11+
Given an integer `num`, repeatedly add all its digits until the result has only one digit, and return it..
12+
13+
### Examples
14+
15+
**Example 1:**
16+
```
17+
Input: num = 38
18+
Output: 2
19+
Explanation: The process is
20+
38 --> 3 + 8 --> 11
21+
11 --> 1 + 1 --> 2
22+
Since 2 has only one digit, return it.
23+
```
24+
**Example 2:**
25+
```
26+
Input: num = 0
27+
Output: 0
28+
```
29+
### Constraints
30+
31+
- `0 <= num <= 2^31 - 1`
32+
33+
### Approach
34+
35+
Any number where it's digits add to `9` is always divisible by `9`. `(18, 27, 36, 45, 54, 63, 72, 81, 90, etc.)` Therefore the 'digital root' for any number divisible by `9` is always `9`. You can see this even in larger numbers like 99 because `9 + 9 = 18`, and then `1 + 8 = 9` still, so the root always becomes `9` for any numbers divisible by `9`.
36+
37+
Additionally, 0 always has a digital root of 0 obviously.
38+
39+
The only other cases you need to worry about to find the digital root are when it isn't 0 or 9.
40+
41+
So for any number that isn't 0 and isn't divisible by 9, the root will always `n % 9` for a given number n. (AKA the difference between given number n and the nearest number that is divisible by `9`, since numbers divisible by `9` always have a digital root of `9`).
42+
For examples: `100 % 9 = 1 (one greater than 99, which is divisible by 9)`.
43+
`101 % 9 = 2`
44+
`102 % 9 = 3` and so on.
45+
46+
This explanation/algorithm skips the whole "add digits until there is only 1 remaining", so the description of this problem seems pretty misleading to me since it makes you think the solution will be something unrelated to the optimal one. I guess the point of Leetcode is to learn all of these tricks though.
47+
48+
49+
50+
### Solution
51+
52+
#### Code in Different Languages
53+
54+
### C++ Solution
55+
```cpp
56+
class Solution {
57+
public:
58+
int addDigits(int num) {
59+
if(num == 0) return 0;
60+
else if(num % 9 == 0) return 9;
61+
else return num % 9;
62+
}
63+
};
64+
```
65+
### Java Solution
66+
```java
67+
class Solution {
68+
public int addDigits(int num) {
69+
if(num == 0) return 0;
70+
else if(num % 9 == 0) return 9;
71+
else return num % 9;
72+
}
73+
}
74+
```
75+
### Python Solution
76+
77+
```python
78+
class Solution:
79+
def addDigits(self, num: int) -> int:
80+
if num == 0 : return 0
81+
if num % 9 == 0 : return 9
82+
else : return (num % 9)
83+
```
84+
### Complexity Analysis
85+
**Time Complexity:** $O(1)$
86+
87+
88+
**Space Complexity:** $O(1)$
89+
90+
91+
This solution efficiently add the digits of a number in $o(1)$ space complexity and $o(1)$ time complexity
92+
93+
### References
94+
**LeetCode Problem:** Add-digits
95+

0 commit comments

Comments
 (0)