Maximum sum obtained by dividing Array into several subarrays as per given conditions | Java program for the above approach ; Function to find the required answer ; Stores maximum sum ; Adding the difference of elements at ends of increasing subarray to the answer ; Driver Code ; Input ; Function calling
import java . io . * ; class GFG { public static int maximumSum ( int arr [ ] , int N ) { int Sum = 0 ; for ( int i = 1 ; i < N ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] ) Sum += ( arr [ i ] - arr [ i - 1 ] ) ; } return Sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 3 } ; int N = arr . length ; System . out . println ( maximumSum ( arr , N ) ) ; } }