Minimum number of steps required to obtain the given Array by the given operations | Function to calculate the minimum steps to obtain the desired array ; Initialize variable ; Iterate over the array arr ; Check if i > 0 ; Update the answer ; Return the result ; Driver Code
function min_operation ( a , n ) { var ans = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( i > 0 ) ans += Math . abs ( a [ i ] - a [ i - 1 ] ) ; else ans += Math . abs ( a [ i ] ) ; } return ans ; } var arr = [ 1 , 2 , 3 , 4 ] ; var n = arr . length ; document . write ( min_operation ( arr , n ) ) ;