Initialize insertions to 0
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 ; }