Minimum number to be added to all digits of X to make X > Y | Function to check if X is lexicographically larger Y ; It is lexicographically larger ; Utility function to check minimum value of d ; If X is already larger do not need to add anything ; Adding d to all elements of X ; If X is larger now print d ; else print d + 1 ; Taking the numbers as sequences
function IsLarger ( X , Y , n ) { for ( let i = 0 ; i < n ; i ++ ) { if ( X [ i ] < Y [ i ] ) { return false ; } } return true ; } function solve ( X , Y , n ) { let ans = 0 ; if ( IsLarger ( X , Y , n ) ) { ans = 0 ; } else { let d = Y [ 0 ] - X [ 0 ] ; for ( let i = 0 ; i < n ; i ++ ) { X [ i ] += d ; } if ( IsLarger ( X , Y , n ) ) { ans = d ; } else { ans = d + 1 ; } } return ans ; } let X = [ 2 , 3 , 6 , 9 ] ; let Y = [ 3 , 4 , 8 , 1 ] ; let n = X . length ; document . write ( solve ( X , Y , n ) ) ;