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
- Find XOR of numbers from the range [L, R]
- Sum of all odd natural numbers in range L and R
- 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 Fibonacci Numbers in a range
- Prime numbers in a given range using STL | Set 2
- Sum of all the prime numbers in a given range
- Count Odd and Even numbers in a range from L to R
- Sum of all numbers divisible by 6 in a given range
Improved By : Mithun Kumar