Maximize difference between sum of even and odd | Function to find the maximum possible difference between sum of even and odd indices ; Convert arr [ ] o 1 - based indexing ; Reverse the array ; Convert arr [ ] o 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
def maxPossibleDiff ( arr , N ) : NEW_LINE INDENT arr . append ( - 1 ) NEW_LINE arr = arr [ : : - 1 ] NEW_LINE arr . append ( - 1 ) NEW_LINE arr = arr [ : : - 1 ] NEW_LINE maxDiff = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT if ( arr [ i ] > arr [ i - 1 ] and arr [ i ] > arr [ i + 1 ] ) : NEW_LINE INDENT maxDiff += arr [ i ] NEW_LINE DEDENT if ( arr [ i ] < arr [ i - 1 ] and arr [ i ] < arr [ i + 1 ] ) : NEW_LINE INDENT maxDiff -= arr [ i ] NEW_LINE DEDENT DEDENT print ( maxDiff ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 2 , 1 , 4 , 5 , 2 , 1 , 7 , 8 , 9 ] NEW_LINE N = len ( arr ) NEW_LINE maxPossibleDiff ( arr , N ) NEW_LINE DEDENT