Find the number of digits
Table of contents
No headings in the article.
To day we are going to look into the basic DSA Problem
Find the number of digits for the given number
let's look how we can solve this problem in different way
we assume all the input given to the program is Greater than Zero and with no leading Zeros for simplicity.
For number with Leading Zeros we have Different Technique which we can see later.
we are going to solve the problem in following methods
- Iteration Method
- Recursive Method
- Using Log10 Method
Let's look into iterative method the most common solution
public int countByIteration(int number) {
int count = 0;
while (number > 0) {
++count;
number = number / 10;
}
return count;
}
consider the given input is 123, we have 3 digits
Here we are initialise a variable count to store the number of digits
int count =0;
we have create a while loop which checks is the given number is greater than Zero.
while(number>0){}
123 is greater than Zero, so it enters inside the while loop
Inside the while loop we are incrementing the count value since the number (123) is not Zero (now count=1)
NOTE:- when you divide 2 integers in java 12/10 its 1 not 1.2(result is int), when you divide 2 double in java 12.00/10.00 its 1.2 not 1(result is double) and if you divide one integer and one double 12.00/10 its 1.2(result is double)
we divide the number(123) by 10, which makes 123 to 12
now while loop is going to check is 12 greater than 0 ,since 12 is greater than 0 it again enters the while loop and increment the count value (now count = 2)
we divide the number(12) by 10, which makes 12 to 1
now while loop is going to check is 1 greater than 0 ,since 1 is greater than 0 it again enters the while loop and increment the count value (now count = 3)
we divide the number(1) by 10, which make 1 to 0
now while loop is going to check is 0 greater than 0, since its not true while loop wont execute
after while loop we have the return statement which is going to return the count(count =3)
Time Complexity : O(n)
Auxiliary Space : O(1)
Now it's time for Recursive Method
public int countByRecursion(int number) {
if (number == 0) {
return 0;
}
return 1 + countByRecursion(number / 10);
}
The base condition is going to check is the number is 0 or not if its Zero its going to return us 0 and stops the recursive call
if not we are making a recursive call by dividing the number by 10 and adding that call result with 1
Time Complexity : O(log(n))
Auxiliary Space : O(log(n))
Now the Log Method
public int countByLog(int number) {
return (int) Math.floor(Math.log10(number) + 1);
}
Looks simple right!!! and this is also the efficient method when compared to countByRecursion and countByIteration
the idea behind this is when we do log10(123) its going to give us 2.089905111439398
if we take floor we get 2.0 and then we add 1 to it so it becomes 3.0 at last we cast that to int so it becomes 3
Time Complexity : O(1)
Auxiliary Space : O(1)
If you are at interview you can use the Log method.
This is a part of my DSA preparation series
Day 1 to 9 : Analysis of Algorithm
Day 10 : Find the number of digits