Maximize difference between sum of even and odd | 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
function maxPossibleDiff ( arr , N ) { arr . push ( - 1 ) ; arr . reverse ( ) ; arr . push ( - 1 ) ; arr . reverse ( ) ; var maxDiff = 0 ; for ( var 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 ] ; } } document . write ( maxDiff ) ; } var arr = [ 3 , 2 , 1 , 4 , 5 , 2 , 1 , 7 , 8 , 9 ] ; var N = arr . length ; maxPossibleDiff ( arr , N ) ;