Problem Statement
You are given an integer n. You need to return the number of digits in the number.
The number will have no leading zeroes, except when the number is 0 itself.
Examples
Example 1:
Input: n = 4
Output: 1
Explanation: There is only 1 digit in 4.
Example 2:
Input: n = 33
Output: 2
Exaplanation: There are 2 digits in 33.
Approaches
1️⃣
Intuition:
To count the digits of a number, the main idea is to repeatedly divide the number by 10 until it becomes 0. Each division operation effectively removes the last digit from the number. We keep a counter to track how many times we perform the division, which will give us the total number of digits.
Approach:
- Initialize a counter to zero to keep track of the number of digits.
- Check if the number is zero. If it is, the number has exactly one digit, so return 1.
- Repeat the following until the number is reduced to zero:
- Increment the counter.
- Divide the number by 10 to remove the last digit.
- The value of the counter will be the number of digits in the input number.
Edge Case:
- What if the given number is zero?
- Return 1, because the number of digits in zero is 1.
Dry Run:
Initially:
N = 3356
count = 0
First Iteration:
lastDigit = N % 10 = 3356 % 10 => 6
N = N/10 = 3356 / 10 = 335
count = count + 1 = 0 + 1 = 1
Second Iteration:
lastDigit = N % 10 = 335 % 10 => 5
N = N/10 = 335 / 10 = 33
count = count + 1 = 1 + 1 = 2
Third Iteration:
lastDigit = N % 10 = 33 % 10 => 3
N = N/10 = 33 / 10 = 3
count = count + 1 = 2 + 1 = 3
Fourth Iteration:
lastDigit = N % 10 = 3 % 10 => 3
N = N/10 = 3 / 10 = 0
count = count + 1 = 3 + 1 = 4
N = 0
count = 4
Since N = 0, hence loop is terminated, return count
Code:
#include <iostream>
int countDigits(int n) {
// Step 1: Handle the special case where n is 0
if (n == 0) {
return 1; // 0 has exactly one digit
}
int count = 0;
// Step 2: Loop until n becomes 0
while (n != 0) {
n = n / 10; // Remove the last digit
count++; // Increment the digit count
}
return count;
}
int main() {
int n;
std::cout << "Enter a number: ";
std::cin >> n;
int result = countDigits(n);
std::cout << "Number of digits: " << result << std::endl;
return 0;
}
Complexity Analysis:
- Time Complexity:
O(log₁₀(n))
- The loop runs once for each digit, meaning the number of iterations is proportional to the number of digits, which is
log₁₀(n)
. - In every iteration we are dividing
n
by 10.
- The loop runs once for each digit, meaning the number of iterations is proportional to the number of digits, which is
- Space Complexity:
O(1)
- The algorithm uses a constant amount of space to store the count and the number itself.
2️⃣
Intuition:
Given a number, the number of digits can be found directly from the logarithm (base 10) of the number.
Logarithm-Based Approach:
The number of digits in a positive integer n
can be found using the following formula:
Number of digits= log10(n) + 1
Here's how it works:
- The logarithm base 10 of a number
n
, denoted aslog₁₀(n)
, gives us the power to which 10 must be raised to getn
. - For example,
log₁₀(1000)
equals3
, because 103=100010^3=1000 . - The integer part of
log₁₀(n)
(i.e., the value oflog₁₀(n)
without the decimal part) tells us how many digits minus one are in the numbern
. To get the actual number of digits, we add 1 to this value.
Approach:
- Instead of iterating on the number for every digit, the count of digits in the number can be found using the following mathematical formula:
count = log10(N) + 1
.
Steps:
- If
n
is0
, return1
because0
has exactly one digit. - If
n
is positive, compute the number of digits using the formula Number of digits =log10(n) + 1
.
Edge Case:
- What if the given number is zero?
- Return 1, because the number of digits in zero is 1.
Code:
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
/* Function to count the
number of odd digits in N */
int countDigit(int n) {
// Edge case
if(n == 0) return 1;
int count = log10(n) + 1;
return count;
}
};
int main()
{
int n = 6678;
/* Creating an instance of
Solution class */
Solution sol;
// Function call to get count of digits in n
int ans = sol.countDigit(n);
cout << "The count of digits in the given number is: " << ans;
return 0;
}
Complexity Analysis:
- Time Complexity:
O(1)
- The logarithm operation is performed in constant time.
- Space Complexity:
O(1)
- The space required is constant, as we're only storing the result.