Minimum Decrements on Subarrays required to reduce all Array elements to zero | Java Program to implement the above approach ; Function to count the minimum number of subarrays that are required to be decremented by 1 ; Base Case ; Initializing ans to first element ; For A [ i ] > A [ i - 1 ] , operation ( A [ i ] - A [ i - 1 ] ) is required ; Return the count ; Driver Code
import java . io . * ; class GFG { static int min_operations ( int A [ ] , int n ) { if ( n == 0 ) return 0 ; int ans = A [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( A [ i ] > A [ i - 1 ] ) { ans += A [ i ] - A [ i - 1 ] ; } } return ans ; } public static void main ( String [ ] args ) { int n = 5 ; int A [ ] = { 1 , 2 , 3 , 2 , 1 } ; System . out . println ( min_operations ( A , n ) ) ; } }