Maximize difference between sum of even and odd | Java program to implement the above approach ; Function to find the maximum possible difference between sum of even and odd indices ; Convert arr [ ] into 1 - based indexing ; Reverse the array ; Convert arr [ ] into 1 based index ; Reverse the array ; Stores maximum difference between sum of even and odd indexed elements ; Traverse the array ; If arr . get ( i ) is local maxima ; Update maxDiff ; If arr . get ( i ) is local minima ; Update maxDiff ; Driver Code ; Size of array ; Function Call
import java . util . * ; class GFG { static void maxPossibleDiff ( Vector < Integer > arr , int N ) { arr . add ( - 1 ) ; Collections . reverse ( arr ) ; arr . add ( - 1 ) ; Collections . reverse ( arr ) ; int maxDiff = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( arr . get ( i ) > arr . get ( i - 1 ) && arr . get ( i ) > arr . get ( i + 1 ) ) { maxDiff += arr . get ( i ) ; } if ( arr . get ( i ) < arr . get ( i - 1 ) && arr . get ( i ) < arr . get ( i + 1 ) ) { maxDiff -= arr . get ( i ) ; } } System . out . print ( maxDiff ) ; } public static void main ( String [ ] args ) { int [ ] array = { 3 , 2 , 1 , 4 , 5 , 2 , 1 , 7 , 8 , 9 } ; Vector < Integer > v = new Vector < > ( ) ; for ( int i : array ) { v . add ( i ) ; } int N = v . size ( ) ; maxPossibleDiff ( v , N ) ; } }