Next greater Number than N with the same quantity of digits A and B | Recursive function to find the required number ; If the resulting number is >= n and count of a = count of b , return the number ; select minimum of two and call the function again ; Function to find the number next greater Number than N with the same quantity of digits A and B ; Driver code
function findNumUtil ( res , a , aCount , b , bCount , n ) { if ( res > 1e11 ) return 1e11 ; if ( aCount == bCount && res >= n ) return res ; return Math . min ( findNumUtil ( res * 10 + a , a , aCount + 1 , b , bCount , n ) , findNumUtil ( res * 10 + b , a , aCount , b , bCount + 1 , n ) ) ; } 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 ) ) ;