|
| 1 | +--- |
| 2 | +id: count-digits |
| 3 | +title: Count Digits |
| 4 | +sidebar_label: Count-Digits |
| 5 | +tags: |
| 6 | + - Modular Arithmetic |
| 7 | + - Algorithms |
| 8 | +description: "This tutorial covers the solution to the Count Digits problem from the GeeksforGeeks." |
| 9 | +--- |
| 10 | +## Problem Description |
| 11 | + |
| 12 | +Given a number `n`. Count the number of digits in `n` which evenly divide n. Return an integer, total number of digits of n which divides n evenly. |
| 13 | + |
| 14 | +Note :- Evenly divides means whether `n` is divisible by a digit i.e. leaves a remainder `0` when divided. |
| 15 | + |
| 16 | +## Examples |
| 17 | + |
| 18 | +**Example 1:** |
| 19 | + |
| 20 | +``` |
| 21 | +Input: n = 12 |
| 22 | +Output: 2 |
| 23 | +Explanation: 1, 2 when both divide 12 leaves remainder 0. |
| 24 | +``` |
| 25 | + |
| 26 | +**Example 2:** |
| 27 | + |
| 28 | +``` |
| 29 | +Input: n = 2446 |
| 30 | +Output: 1 |
| 31 | +Explanation: Here among 2, 4, 6 only 2 divides 2446 evenly while 4 and 6 do not. |
| 32 | +``` |
| 33 | + |
| 34 | + |
| 35 | +Expected Time Complexity: O(N) |
| 36 | + |
| 37 | +Expected Auxiliary Space: O(1) |
| 38 | + |
| 39 | +## Constraints |
| 40 | + |
| 41 | +* `1 ≤ N ≤ 10^5` |
| 42 | + |
| 43 | +## Problem Explanation |
| 44 | + |
| 45 | +The task is to traverse the number and count the digits. |
| 46 | + |
| 47 | +## Code Implementation |
| 48 | + |
| 49 | +### C++ Solution |
| 50 | + |
| 51 | +```cpp |
| 52 | +int countDigits(int n) { |
| 53 | + int count = 0; |
| 54 | + int temp = n; |
| 55 | + while (temp != 0) { |
| 56 | + int digit = temp % 10; |
| 57 | + if (n % digit == 0) { |
| 58 | + count++; |
| 59 | + } |
| 60 | + temp /= 10; |
| 61 | + } |
| 62 | + return count; |
| 63 | +} |
| 64 | + |
| 65 | + |
| 66 | +``` |
| 67 | +
|
| 68 | +```java |
| 69 | +
|
| 70 | +public int countDigits(int n) { |
| 71 | + int count = 0; |
| 72 | + int temp = n; |
| 73 | + while (temp != 0) { |
| 74 | + int digit = temp % 10; |
| 75 | + if (n % digit == 0) { |
| 76 | + count++; |
| 77 | + } |
| 78 | + temp /= 10; |
| 79 | + } |
| 80 | + return count; |
| 81 | +} |
| 82 | +
|
| 83 | +
|
| 84 | +``` |
| 85 | + |
| 86 | +```python |
| 87 | +def count_digits(n): |
| 88 | + count = 0 |
| 89 | + temp = n |
| 90 | + while temp != 0: |
| 91 | + digit = temp % 10 |
| 92 | + if n % digit == 0: |
| 93 | + count += 1 |
| 94 | + temp //= 10 |
| 95 | + return count |
| 96 | + |
| 97 | +``` |
| 98 | + |
| 99 | +```javascript |
| 100 | +function countDigits(n) { |
| 101 | + let count = 0; |
| 102 | + let temp = n; |
| 103 | + while (temp !== 0) { |
| 104 | + const digit = temp % 10; |
| 105 | + if (n % digit === 0) { |
| 106 | + count++; |
| 107 | + } |
| 108 | + temp = Math.floor(temp / 10); |
| 109 | + } |
| 110 | + return count; |
| 111 | +} |
| 112 | + |
| 113 | + |
| 114 | +``` |
| 115 | + |
| 116 | +## Solution Logic: |
| 117 | +1. Initialize a variable count to 0, which will store the number of digits that evenly divide n. |
| 118 | +2. Initialize a variable temp to n, which will be used to iterate through each digit of n. |
| 119 | +3. Use a while loop to iterate through each digit of n. In each iteration, do the following: |
| 120 | + - Calculate the current digit by taking the remainder of temp divided by 10 (temp % 10). |
| 121 | + - Check if n is divisible by the current digit by checking if n % digit == 0. If it is, increment count. |
| 122 | + - Update temp by dividing it by 10 (temp /= 10). |
| 123 | +4. Return count at the end of the function. |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | +## Time Complexity |
| 128 | + |
| 129 | +* The time complexity is $O(log(n))$ where n is the input number. This is because we are iterating through each digit of n using a while loop, and the number of digits in n is proportional to the logarithm of n. |
| 130 | + |
| 131 | + |
| 132 | +## Space Complexity |
| 133 | + |
| 134 | +* The auxiliary space complexity is $O(1)$ due to the only extra memory used is for temporary variables while swapping two values in Array. |
0 commit comments