Next greater Number than N with the same quantity of digits A and B

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
Examples:
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:
C++
// C++ program to find next greater Number// than N with the same quantity of// digits A and B#include <bits/stdc++.h>using namespace std;// Recursive function to find the required numberlong findNumUtil(long res, int a, int aCount, int b, int bCount, int n){ if (res > 1e11) return 1e11; // If the resulting number is >= n and // count of a = count of b, return the number if (aCount == bCount && res >= n) return res; // 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 Bint findNum(int n, int a, int b){ int result = 0; int aCount = 0; int bCount = 0; return findNumUtil(result, a, aCount, b, bCount, n);}// Driver codeint main(){ int N = 4500; int A = 4; int B = 7; cout << findNum(N, A, B); return 0;} |
Java
// Java program to find next greater Number// than N with the same quantity of// digits A and Bpublic class GFG { // Recursive function to find the required number static long findNumUtil(long res, int a, int aCount, int b, int bCount, int n) { if (res > 1e11) return (long) 1e11; // If the resulting number is >= n and // count of a = count of b, return the number if (aCount == bCount && res >= n) return res; // select minimum of two and call the function again return Math.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 static int findNum(int n, int a, int b) { int result = 0; int aCount = 0; int bCount = 0; return (int) findNumUtil(result, a, aCount, b, bCount, n); } // Driver code public static void main(String args[]) { int N = 4500; int A = 4; int B = 7; System.out.println(findNum(N, A, B)); } // This Code is contributed by ANKITRAI1} |
Python3
# 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 numberdef findNumUtil(res, a, aCount, b, bCount, n): if (res > 1e11): return 1e11 # If the resulting number is >= n # and count of a = count of b, # return the number if (aCount == bCount and res >= n): return res # 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 Bdef findNum(n, a, b): result = 0 aCount = 0 bCount = 0 return findNumUtil(result, a, aCount, b, bCount, n)# Driver codeif __name__ == '__main__': N = 4500 A = 4 B = 7 print(findNum(N, A, B))# This code is contributed by# Sanjit_Prasad |
C#
// C# program to find next greater Number// than N with the same quantity of// digits A and Busing System;class GFG{// Recursive function to find the required numberstatic long findNumUtil(long res, int a, int aCount, int b, int bCount, int n){ if (res > 1e11) return (long) 1e11; // If the resulting number is >= n and // count of a = count of b, return the number if (aCount == bCount && res >= n) return res; // select minimum of two and call // the function again return Math.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 Bstatic int findNum(int n, int a, int b){ int result = 0; int aCount = 0; int bCount = 0; return (int) findNumUtil(result, a, aCount, b, bCount, n);}// Driver codepublic static void Main(){ int N = 4500; int A = 4; int B = 7; Console.WriteLine(findNum(N, A, B));}}// This code is contributed by Shashank |
PHP
<?php// PHP program to find next greater Number// than N with the same quantity of// digits A and B// Recursive function to find the required numberfunction findNumUtil($res, $a, $aCount, $b, $bCount, $n){ if ($res > 100000000000) return 10000000000; // If the resulting number is >= n and // count of a = count of b, return the number if ($aCount == $bCount && $res >= $n) return $res; // 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 Bfunction findNum($n, $a, $b){ $result = 0; $aCount = 0; $bCount = 0; return findNumUtil($result, $a, $aCount, $b, $bCount, $n);}// Driver code $N = 4500; $A = 4; $B = 7; echo findNum($N, $A, $B);// This Code is contributed by mits?> |
Javascript
<script> // Javascript program to find next greater Number // than N with the same quantity of // digits A and B // Recursive function to find the required number function findNumUtil(res, a, aCount, b, bCount, n) { if (res > 1e11) return 1e11; // If the resulting number is >= n and // count of a = count of b, return the number if (aCount == bCount && res >= n) return res; // select minimum of two and call // the function again return Math.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 function findNum(n, a, b) { let result = 0; let aCount = 0; let bCount = 0; return findNumUtil(result, a, aCount, b, bCount, n); } let N = 4500; let A = 4; let B = 7; document.write(findNum(N, A, B)); // This code is contributed by divyesh072019.</script> |
Output:
4747
Time Complexity: O(2n)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



