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
- Print prime numbers in a given range using C++ STL
- Find the good permutation of first N natural numbers
- Print N lines of 4 numbers such that every pair among 4 numbers has a GCD K
- Print factorials of a range in right aligned format
- Segmented Sieve (Print Primes in a Range)
- Sum of all even numbers in range L and R
- Sum of all numbers divisible by 6 in a given range
- Prime numbers in a given range using STL | Set 2
- Sum of Fibonacci Numbers in a range
- Sum of all natural numbers in range L to R
- Check whether XOR of all numbers in a given range is even or odd
- Sum of all odd natural numbers in range L and R
- Sum of all odd factors of numbers in the range [l, r]
- Count of numbers having only 1 set bit in the range [0, n]
- Sum of all even factors of numbers in the range [l, r]
Improved By : Mithun Kumar