Maximum prefix sum after K reversals of a given array | Java 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 ; Print the answer ; Driver Code ; Function Call
import java . io . * ; import java . util . * ; class GFG { static void maxSumAfterKReverse ( Integer arr [ ] , int K , int N ) { int sum = Integer . MIN_VALUE ; if ( K % 2 != 0 ) Collections . reverse ( Arrays . asList ( arr ) ) ; int currsum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { currsum += arr [ i ] ; sum = Math . max ( sum , currsum ) ; } System . out . print ( sum ) ; } public static void main ( String [ ] args ) { Integer [ ] arr = { 1 , 5 , 8 , 9 , 11 , 2 } ; int K = 1 ; int N = arr . length ; maxSumAfterKReverse ( arr , K , N ) ; } }