Given two positive integers L and R which represents a range and two more positive integers d and K. The task is to find the count of numbers in the range where digit d occurs exactly K times.
Input: L = 11, R = 100, d = 2, k = 1
Required numbers are 12, 20, 21, 23, 24, 25, 26, 27, 28, 29, 32, 42, 52, 62, 72, 82 and 92.
Input: L = 95, R = 1005, d = 0, k = 2
Prerequisites : Digit DP
Approach: Firstly, if we are able to count the required numbers upto R i.e. in the range [0, R], we can easily reach our answer in the range [L, R] by solving for from zero to R and then subtracting the answer we get after solving for from zero to L – 1. Now, we need to define the DP states.
In the final recursive call, when we are at the last position if the count of digit d is equal to K, return 1 otherwise return 0.
Below is the implementation of the above approach:
- Number of times the given string occurs in the array in the range [l, r]
- Sum of all numbers formed having 4 atmost X times, 5 atmost Y times and 6 atmost Z times
- Count of N-digit numbers having digit XOR as single digit
- Smallest integer greater than n such that it consists of digit m exactly k times
- Find number of times a string occurs as a subsequence in given string
- Element which occurs consecutively in a given subarray more than or equal to K times
- Count of Numbers in Range where first digit is equal to last digit of the number
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Generate a number such that the frequency of each digit is digit times the frequency in given number
- Find all numbers between range L to R such that sum of digit and sum of square of digit is prime
- Count numbers with exactly K non-zero digits and distinct odd digit sum
- Count of pairs (A, B) in range 1 to N such that last digit of A is equal to the first digit of B
- Count of Binary strings of length N having atmost M consecutive 1s or 0s alternatively exactly K times
- Count of subarrays which contains a given number exactly K times
- Count n digit numbers not having a particular digit
- String which when repeated exactly K times gives a permutation of S
- Largest element in the array that is repeated exactly k times
- Queries to check whether all the elements can be made positive by flipping signs exactly K times
- Find Nth smallest number that is divisible by 100 exactly K times
- Check if original Array is retained after performing XOR with M exactly K times
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.