Given a number and two digits and . The task is to find the least number not less than N which contains the equal number of digits A and B.
Note: N <= 107
Input : N = 4500, A = 4, B = 7
Output : 4747
The number greater than 4500 which has the same quantity of number ‘4’ and number ‘7’ is 4747.
Input : N = 99999999, A = 6, B = 7
Output : 6666677777
Below is the step by step algorithm to solve this problem:
- If the length of ‘N’ is odd then the resulting number will be of length ‘N+1’ as both ‘a’ and ‘b’ has to be in equal quantity.
- If the length of ‘N’ is even then the resulting number will either be of length ‘N’ or ‘N+2’.
- We will generate the number recursively by appending both A and B one by one and take the minimum of the two for the next recursive call.
- At last return the smallest number greater than or equal to ‘N’.
Below is the implementation of the above idea:
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.
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Next higher palindromic number using the same set of digits
- Find largest number smaller than N with same set of digits
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Find the average of k digits from the beginning and l digits from the end of the given number
- Maximize the given number by replacing a segment of digits with the alternate digits given
- How to store a very large number of more than 100 digits in C++
- Find maximum product of digits among numbers less than or equal to N
- Next higher number using atmost one swap operation
- Minimum number with digits as 4 and 7 only and given sum
- Find the smallest number whose digits multiply to a given number n
- Get the kth smallest number using the digits of the given number
- Smallest number with sum of digits as N and divisible by 10^N
- Find the number of integers from 1 to n which contains digits 0's and 1's only
- Check if the number is even or odd whose digits and base (radix) is given
- Number of triplets such that each value is less than N and each pair sum is a multiple of K
- Find two numbers with difference and division both same as N
- Count ways to spell a number with repeated digits
- Number of digits in the product of two numbers
- Smallest number k such that the product of digits of k is equal to n
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.