Given a number n, write a function that returns count of numbers from 1 to n that don’t contain digit 3 in their decimal representation.
Input: n = 10 Output: 9 Input: n = 45 Output: 31 // Numbers 3, 13, 23, 30, 31, 32, 33, 34, // 35, 36, 37, 38, 39, 43 contain digit 3. Input: n = 578 Output: 385
We strongly recommend that you click here and practice it, before moving on to the solution.
We can solve it recursively. Let count(n) be the function that counts such numbers.
'msd' --> the most significant digit in n 'd' --> number of digits in n. count(n) = n if n < 3 count(n) = n - 1 if 3 <= n 10 and msd is not 3 count(n) = count( msd * (10^(d-1)) - 1) if n > 10 and msd is 3
Let us understand the solution with n = 578. count(578) = 4*count(99) + 4 + count(78) The middle term 4 is added to include numbers 100, 200, 400 and 500. Let us take n = 35 as another example. count(35) = count (3*10 - 1) = count(29)
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.
- Find a Symmetric matrix of order N that contain integers from 0 to N-1 and main diagonal should contain only 0's
- Count of Numbers in Range where the number does not contain more than K non zero digits
- Number of n digit numbers that do not contain 9
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Find two numbers whose sum is N and does not contain any digit as K
- Count of integers of length N and value less than K such that they contain digits only from the given set
- Count substrings that contain all vowels | SET 2
- Find the Number which contain the digit d
- Maximum number of segments that can contain the given points
- Find whether only two parallel lines contain all coordinates points or not
- Check if there exists a permutation of given string which doesn't contain any monotonous substring
- 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
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Count numbers which can be constructed using two numbers
- Count numbers which are divisible by all the numbers from 2 to 10
- Count of numbers upto M divisible by given Prime Numbers
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Maximize count of equal numbers in Array of numbers upto N by replacing pairs with their sum
- Count prime numbers that can be expressed as sum of consecutive prime numbers
- Count numbers < = N whose difference with the count of primes upto them is > = K