Given a digit ‘d’ and a range [L, R] where L < R, print all good numbers in given range that don't contain digit 'd'. A number is good if its every digit is larger than the sum of digits which are on the right side of that digit. For example 9620 is good number because 2 > 0, 6 > 2+0 and 9 > 6+2+0.
Input: L = 410, R = 520, d = 3 Output: 410 420 421 510 520 All the numbers in output are good (every digit is more than sum of digits on right of it) and don't have digit 3. Input: L = 410, R = 520, d = 1 Output: 420 430 520 All the numbers in output are good (every digit is more than sum of digits on right of it) and don't have digit 1.
The idea is to traverse all numbers in given range. For every number, traverse all digits. While traversing keep track of digit sum so far. At any point, if previous sum becomes more than or equal to sum, return false. Also, if current digit becomes ‘d’, return false.
Below is implementation of the idea.
410 420 421 510 520
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 all good indices in the given Array
- Find the good permutation of first N natural numbers
- Find the largest good number in the divisors of given number N
- Print all numbers in given range having digits in strictly increasing order
- Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
- Minimum number of elements that should be removed to make the array good
- Find the shortest distance between any pair of two different good nodes
- Find the number of good permutations
- Find the minimum number of elements that should be removed to make an array good
- Sum of Digits of the Good Strings
- Check whether a number is Good prime or not
- Print prime numbers in a given range using C++ STL
- Print even and odd numbers in a given range using recursion
- Print all perfect squares from the given range
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- Queries for GCD of all numbers of an array except elements in a given range
- Sum of all numbers divisible by 6 in a given range
- Sum of all the prime numbers in a given range
- Check whether XOR of all numbers in a given range is even or odd
- Sum of all numbers in the given range which are divisible by M
Improved By : Mithun Kumar