Maximum prefix sum after K reversals of a given array | 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 ; Print the answer ; Driver Code ; Function Call
function maxSumAfterKReverse ( arr , K , N ) { let sum = Number . MIN_VALUE ; if ( K % 2 != 0 ) arr . reverse ( ) ; let currsum = 0 ; for ( let i = 0 ; i < N ; i ++ ) { currsum += arr [ i ] ; sum = Math . max ( sum , currsum ) ; } document . write ( sum ) ; } let arr = [ 1 , 5 , 8 , 9 , 11 , 2 ] ; let K = 1 ; let N = arr . length ; maxSumAfterKReverse ( arr , K , N ) ;