Minimum number of steps required to obtain the given Array by the given operations | Java Program to implement the above approach ; 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
import java . util . * ; class GFG { static int min_operation ( int a [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i > 0 ) ans += Math . abs ( a [ i ] - a [ i - 1 ] ) ; else ans += Math . abs ( a [ i ] ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int n = arr . length ; System . out . print ( min_operation ( arr , n ) ) ; } }