Minimum array insertions required to make consecutive difference <= K | Function to return minimum number of insertions required ; Initialize insertions to 0 ; return total insertions ; Driver Code
function minInsertions ( H , n , K ) { var inser = 0 ; for ( var i = 1 ; i < n ; ++ i ) { var diff = Math . abs ( H [ i ] - H [ i - 1 ] ) ; if ( diff <= K ) continue ; else inser += Math . ceil ( diff / K ) - 1 ; } return inser ; } var H = [ 2 , 4 , 8 , 16 ] ; var K = 3 ; var n = H . length ; document . write ( minInsertions ( H , n , K ) ) ;