We are given two integers n and d, we need to count all n digit numbers that do not have digit d.
Input : n = 2, d = 7 Output : 72 All two digit numbers that don't have 7 as digit are 10, 11, 12, 13, 14, 15, 16, 18, ..... Input : n = 3, d = 9 Output : 648
A simple solution is to traverse through all d digit numbers. For every number, check if it has x as digit or not.
Efficient approach In this method, we check first if excluding digit d is zero or non-zero. If it is zero then, we have 9 numbers (1 to 9) as first number otherwise we have 8 numbers(excluding x digit and 0). Then for all other digits, we have 9 choices i.e (0 to 9 excluding d digit). We simple call digitNumber function with n-1 digits as first number we already find it can be 8 or 9 and simple multiply it. For other numbers we check if digits are odd or even if it is odd we call digitNumber function with (n-1)/2 digits and multiply it by 9 oterwise we call digitNumber function with n/2 digits and store them in result and take result square.
Number from 1 to 7 excluding digit 9. digits multiple number 1 8 8 2 8*9 72 3 8*9*9 648 4 8*9*9*9 5832 5 8*9*9*9*9 52488 6 8*9*9*9*9*9 472392 7 8*9*9*9*9*9*9 4251528
As we can see, in each step we are half the number of digits. Suppose we have 7 digits in this we call function from main with 6(7-1) digits. when we half the digits we left with 3(6/2) digits. Because of this we have to multiply result due to 3 digits with itself to get result for 6 digits. Similarly for 3 digits we have odd digits, we have odd digits. We find result with 1 ((3-1)/2) digits and find result square and multiply it with 9, because we find result for d-1 digits.
Time Complexity : O(log n).
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 the number of occurrences of a particular digit in a number
- 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 numbers having 0 as a digit
- 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 three-digit numbers having difference X with its reverse
- 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 of all possible numbers not exceeding M having suffix N
- Count of Numbers in Range where first digit is equal to last digit of the number
- Count of N-digit numbers with absolute difference of adjacent digits not exceeding K
- Count of N-digit numbers with absolute difference of adjacent digits not exceeding K | Set 2
- Seating arrangement of N boys sitting around a round table such that two particular boys sit together
- Total ways of selecting a group of X men from N men with or without including a particular man
- Check whether an array of strings can correspond to a particular number X
- Numbers having difference with digit sum more than s
- Generate all N digit numbers having absolute difference as K between adjacent digits
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : manishshaw1