Maximize difference between sum of even and odd | C # 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 [ i ] is local maxima ; Update maxDiff ; If arr [ i ] is local minima ; Update maxDiff ; Driver Code ; Size of array ; Function Call
using System ; using System . Collections . Generic ; public class GFG { static void maxPossibleDiff ( List < int > arr , int N ) { arr . Add ( - 1 ) ; arr . Reverse ( ) ; arr . Add ( - 1 ) ; arr . Reverse ( ) ; int maxDiff = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] && arr [ i ] > arr [ i + 1 ] ) { maxDiff += arr [ i ] ; } if ( arr [ i ] < arr [ i - 1 ] && arr [ i ] < arr [ i + 1 ] ) { maxDiff -= arr [ i ] ; } } Console . Write ( maxDiff ) ; } public static void Main ( String [ ] args ) { int [ ] array = { 3 , 2 , 1 , 4 , 5 , 2 , 1 , 7 , 8 , 9 } ; List < int > v = new List < int > ( ) ; foreach ( int i in array ) { v . Add ( i ) ; } int N = v . Count ; maxPossibleDiff ( v , N ) ; } }