Maximum prefix sum after K reversals of a given array | Python3 program for the above approach ; Function to find the maximum prefix sum after K reversals of the array ; Stores the required sum ; If K is odd , reverse the array ; Store current prefix sum of array ; Traverse the array arr [ ] ; Add arr [ i ] to currsum ; Update maximum prefix sum till now ; Prthe answer ; Driver Code ; Function Call
import sys NEW_LINE def maxSumAfterKReverse ( arr , K , N ) : NEW_LINE INDENT sum = - sys . maxsize - 1 NEW_LINE if ( K & 1 ) : NEW_LINE INDENT arr . reverse ( ) NEW_LINE DEDENT currsum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT currsum += arr [ i ] NEW_LINE sum = max ( sum , currsum ) NEW_LINE DEDENT print ( sum ) NEW_LINE DEDENT arr = [ 1 , 5 , 8 , 9 , 11 , 2 ] NEW_LINE K = 1 NEW_LINE N = len ( arr ) NEW_LINE maxSumAfterKReverse ( arr , K , N ) NEW_LINE