Make the list non | Javascript implementation of the approach ; Function to return the minimum element from the range [ prev , MAX ] such that it differs in at most 1 digit with cur ; To start with the value we have achieved in the last step ; Store the value with which the current will be compared ; Current value ; There are at most 4 digits ; If the current digit differs in both the numbers ; Eliminate last digits in both the numbers ; If the number of different digits is at most 1 ; If we can 't find any number for which  the number of change is less than or  equal to 1 then return -1 ; Function to get the non - decreasing list ; Creating a vector for the updated list ; Let 's assume that it is possible to  make the list non-decreasing ; Element of the original array ; Can 't make the list non-decreasing ; If possible then print the list ; Driver code
var DIGITS = 4 , MIN = 1000 , MAX = 9999 ; function getBest ( prev , cur ) { var maximum = Math . max ( MIN , prev ) ; for ( var i = maximum ; i <= MAX ; i ++ ) { var cnt = 0 ; var a = i ; var b = cur ; for ( var k = 0 ; k < DIGITS ; k ++ ) { if ( a % 10 != b % 10 ) cnt += 1 ; a = parseInt ( a / 10 ) ; b = parseInt ( b / 10 ) ; } if ( cnt <= 1 ) return i ; } return - 1 ; } function getList ( arr , n ) { var myList = [ ] ; var i , cur ; var possible = true ; myList . push ( 0 ) ; for ( i = 0 ; i < n ; i ++ ) { cur = arr [ i ] ; myList . push ( getBest ( myList [ myList . length - 1 ] , cur ) ) ; if ( myList [ myList . length - 1 ] == - 1 ) { possible = false ; break ; } } if ( possible ) { for ( i = 1 ; i < myList . length ; i ++ ) document . write ( myList [ i ] + " " ) ; } else document . write ( " " ) ; } var arr = [ 1095 , 1094 , 1095 ] ; var n = arr . length ; getList ( arr , n ) ;