Problem overview
The function noOfDigits receives a non-negative integer n, up to 1,000,000,000, and returns how many digits it is written with. The number 542 has three digits, 100 has three (the zeros count), and 0 has one.
Approach
In an int, dividing by 10 drops the last digit: 542 / 10 is 54. So the number of digits is the number of times you can divide by 10 before reaching 0. A loop that divides by 10 and adds 1 to a counter on each pass does exactly that.
There is one number this loop can't handle. For 0, the condition n > 0 is false from the start, so the loop would count no digits, but 0 is written with one. The function therefore deals with 0 first, before the loop.
The loop on n = 542 looks like this, with the count after each pass:
n=54 count=1
n=5 count=2
n=0 count=3
This kind of loop, where the number of passes depends on the size of the value, is common. A value below 1,000,000,000 has at most nine digits, so the loop never runs more than ten times, whatever the input. The cost grows with the number of digits, not with the value itself.
Step by step
- Handle zero first. If
n == 0, return 1. - Create
countas anintstarting at 0. No digit has been removed yet. - Start a
whileloop that runs whilen > 0. It will run once for every digit. - Add 1 to
countwith++. One pass is one digit. - Replace
nwithn / 10. When the last digit is gone,nis 0 and the loop stops. - Return
count.
The solution in C++
class Solution {
public:
int noOfDigits(int n) {
if (n == 0) {
return 1;
}
int count = 0;
while (n > 0) {
count++;
n = n / 10;
}
return count;
}
};
A few details:
- Integer division. Both
nand10areint, so/discards the remainder: 542 becomes 54, then 5, then 0. That is three passes. count++. The increment operator adds 1 to the variable. Here it is used as a statement on its own, socount++and++countdo the same thing.nis a copy. The function receives its argument by value, so changingninside doesn't change the caller's variable.
Other ways to count digits
Two other ideas come up often, and it helps to know why the lesson doesn't use them.
- Converting to text.
to_string(n).size()builds a string of the number's digits and returns its length. It gives 3 for 542, 3 for 100, 1 for 0 and 10 for 1000000000, so it handles zero without a special case. It allocates a string, though, and the lesson is about practising the loop. - Logarithms.
floor(log10(n)) + 1also gives the right count for positive numbers, butlog10(0)is minus infinity, so zero needs its own check again. It also moves the work into floating-point arithmetic, which is something to avoid when exact integer counts are needed.
The division loop uses only integers and shows directly what "a digit" means for a number stored as a value: something you can remove with / 10.
A loop that runs at least once
The special case for 0 can also be avoided with a do ... while loop, which checks its condition after the body, so the body always runs once:
int count = 0;
do {
count++;
n = n / 10;
} while (n > 0);
With this loop, 0 counts one digit, then becomes 0 again and stops. It returns 3, 3, 1, 1 and 10 for 542, 100, 0, 7 and 1000000000. The lesson's version keeps the zero check separate, which makes the reason for it visible.
Complexity
Time: O(d), where d is the number of digits of n. Each pass removes one digit and does a constant amount of work. In other words the time is proportional to the logarithm of n, not to n.
Space: O(1). The function uses one counter and its own copy of n.
Edge cases and common mistakes
- Zero. Without the first
if,noOfDigits(0)would return 0. With it, the answer is 1. - Numbers with zeros. For 100 the loop passes three times: 100 becomes 10, then 1, then 0. The result is 3.
- The largest input. 1000000000 has ten digits, and 999999999, just below it, has nine. Both fit in an
int. - Single digits. 7 becomes 0 after one division, so the answer is 1.
- Dividing the wrong way. If the loop forgets
n = n / 10,nnever reaches 0 and the loop doesn't end.
Testing
n = 542gives 3.n = 100gives 3.n = 0gives 1.n = 7gives 1.n = 999999999gives 9.n = 1000000000gives 10.
