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:
# Python 3 program to find next greater
# Number than N with the same quantity of
# digits A and B
# Recursive function to find the
# required number
def findNumUtil(res, a, aCount, b, bCount, n):
if (res > 1e11):
# If the resulting number is >= n
# and count of a = count of b,
# return the number
if (aCount == bCount and res >= n):
# select minimum of two and call
# the function again
return min(findNumUtil(res * 10 + a,
a, aCount + 1, b, bCount, n),
findNumUtil(res * 10 + b, a,
aCount, b, bCount + 1, n))
# Function to find the number next
# greater Number than N with the
# same quantity of digits A and B
def findNum(n, a, b):
result = 0
aCount = 0
bCount = 0
return findNumUtil(result, a, aCount,
b, bCount, n)
# Driver code
if __name__ == ‘__main__’:
N = 4500
A = 4
B = 7
print(findNum(N, A, B))
# This code is contributed by
- Find next greater number with same set of digits
- Largest number not greater than N all the digits of which are odd
- Largest number not greater than N which can become prime after rearranging its digits
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Maximize the given number by replacing a segment of digits with the alternate digits given
- Find the Largest number with given number of digits and sum of digits
- Program to find the quantity after mixture replacement
- Number of digits in the nth number made of given four digits
- Check whether all the rotations of a given number is greater than or equal to the given number or not
- Least Greater number with same digit sum
- Perfect cube greater than a given number
- Largest even digit number not greater than N
- Next greater number than N with exactly one bit different in binary representation of N
- Number of times a number can be replaced by the sum of its digits until it only contains one digit
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.