Largest number not exceeding N that does not contain any of the digits of S | Function to obtain the largest number not exceeding num which does not contain any digits present in S ; Stores digits of S ; Traverse the string S ; Set occurrence as true ; Traverse the string n ; All digits of num are not present in string s ; Largest Digit not present in the string s ; Set all digits from positions in + 1 to n - 1 as LargestDig ; Counting leading zeroes ; Removing leading zeroes ; If the string becomes null ; Return the largest number ; Driver Code
function greatestReducedNumber ( num , s ) { let vis_s = [ false , false , false , false , false , false , false , false , false , false ] ; for ( let i = 0 ; i < s . length ; i ++ ) { vis_s [ Number ( s [ i ] ) - 48 ] = true ; } let n = num . length ; let inn = - 1 ; for ( let i = 0 ; i < n ; i ++ ) { if ( vis_s [ Number ( num [ i ] ) - ' ' ] ) { inn = i ; break ; } } if ( inn == - 1 ) { return num ; } for ( let dig = String ( num [ inn ] ) ; dig >= ' ' ; dig -- ) { if ( vis_s [ Number ( dig ) - ' ' ] == 0 ) { num [ inn ] = dig ; break ; } } let LargestDig = ' ' ; for ( let dig = ' ' ; dig >= ' ' ; dig -- ) { if ( vis_s [ dig - ' ' ] == false ) { LargestDig = dig ; break ; } } for ( let i = inn + 1 ; i < n ; i ++ ) { num [ i ] = LargestDig ; } let Count = 0 ; for ( let i = 0 ; i < n ; i ++ ) { if ( num [ i ] == ' ' ) Count ++ ; else break ; } num = Number ( num ) . toString ( ) ; if ( num . length == 0 ) return " " ; return num ; } let N = " " ; let S = " " ; document . write ( greatestReducedNumber ( N , S ) ) ;