Problem: Count how many integers from 1 to N contains 0 as a digit.
Input: n = 9 Output: 0 Input: n = 107 Output: 17 The numbers having 0 are 10, 20,..90, 100, 101..107 Input: n = 155 Output: 24 The numbers having 0 are 10, 20,..90, 100, 101..110, 120, ..150.
A naive solution is discussed in previous post
In this post an optimized solution is discussed. Let’s analyze the problem closely.
Let the given number has d digits .
The required answer can be computed by computing the following two values:
- Count of 0 digit integers having maximum of d-1 digits.
- Count of 0 digit integers having exactly d digits (less than/ equal to the given number of course!)
Therefore, the solution would be the sum of above two.
The first part has already been discussed here.
How to find the second part?
We can find the total number of integers having d digits (less than equal to given number), which don’t contain any zero.
To find this we traverse the number, one digit at a time.
We find count of non-negative integers as follows:
- If the number at that place is zero, decrement counter by 1 and break (because we can’t move any further, decrement to assure that the number itself contains a zero)
- else , multiply the (number-1), with power(9, number of digits to the right to it)
Let’s illustrate with an example.
Let the number be n = 123. non_zero = 0 We encounter 1 first, add (1-1)*92 to non_zero (= 0+0) We encounter 2, add (2-1)*91 to non_zero (= 0+9 = 9) We encounter 3, add (3-1)*90 to non_zero (=9+3 = 12)
We can observe that non_zero denotes the number of integer consisting of 3 digits (not greater than 123) and don’t contain any zero. i.e., (111, 112, ….., 119, 121, 122, 123) (It is recommended to verify it once)
Now, one may ask what’s the point of calculating the count of numbers which don’t have any zeroes?
Correct! we’re interested to find the count of integers which have zero.
However, we can now easily find that by subtracting non_zero from n after ignoring the most significant place.i.e., In our previous example zero = 23 – non_zero = 23-12 =11 and finally we add the two parts to arrive at the required result!!
Below is implementation of above idea.
Count of numbers from 1 to 107 is 17 Count of numbers from 1 to 1264 is 315
Time Complexity : O(d), where d is no. of digits i.e., O(log(n)
Auxiliary Space : O(1)
This article is contributed by Ashutosh Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Count of N-digit numbers having digit XOR as single digit
- Count n digit numbers not having a particular digit
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Count of Numbers in Range where first digit is equal to last digit of the number
- Count numbers having 0 as a digit
- Count numbers formed by given two digit with sum having given digits
- Count of N digit Numbers having no pair of equal consecutive Digits
- Count all numbers up to N having M as the last digit
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Count 'd' digit positive integers with 0 as a digit
- Count of pairs (A, B) in range 1 to N such that last digit of A is equal to the first digit of B
- Numbers having difference with digit sum more than s
- N digit numbers having difference between the first and last digits as K
- Generate all N digit numbers having absolute difference as K between adjacent digits
- Count non-palindromic array elements having same first and last digit
- Check if frequency of each digit is less than the digit
- Largest number less than N with digit sum greater than the digit sum of N
- Find the remainder when First digit of a number is divided by its Last digit
- Last digit of a number raised to last digit of N factorial