K maximum sums of overlapping contiguous sub | Function to compute prefix - sum of the input array ; Update maxi by k maximum values from maxi and cand ; Here cand and maxi arrays are in non - increasing order beforehand . Now , j is the index of the next cand element and i is the index of next maxi element . Traverse through maxi array . If cand [ j ] > maxi [ i ] insert cand [ j ] at the ith position in the maxi array and remove the minimum element of the maxi array i . e . the last element and increase j by 1 i . e . take the next element from cand . ; Insert prefix_sum [ i ] to mini array if needed ; Traverse the mini array from left to right . If prefix_sum [ i ] is less than any element then insert prefix_sum [ i ] at that position and delete maximum element of the mini array i . e . the rightmost element from the array . ; Function to compute k maximum overlapping sub - array sums ; Compute the prefix sum of the input array . ; Set all the elements of mini as + infinite except 0 th . Set the 0 th element as 0. ; Set all the elements of maxi as - infinite . ; Initialize cand array . ; For each element of the prefix_sum array do : compute the cand array . take k maximum values from maxi and cand using maxmerge function . insert prefix_sum [ i ] to mini array if needed using insertMini function . ; Elements of maxi array is the k maximum overlapping sub - array sums . Print out the elements of maxi array . ; Test case 1 ; Test case 2
def prefix_sum ( arr , n ) : NEW_LINE INDENT pre_sum = list ( ) NEW_LINE pre_sum . append ( arr [ 0 ] ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT pre_sum . append ( pre_sum [ i - 1 ] + arr [ i ] ) NEW_LINE DEDENT return pre_sum NEW_LINE DEDENT def maxMerge ( maxi , cand ) : NEW_LINE INDENT k = len ( maxi ) NEW_LINE j = 0 NEW_LINE for i in range ( k ) : NEW_LINE INDENT if ( cand [ j ] > maxi [ i ] ) : NEW_LINE INDENT maxi . insert ( i , cand [ j ] ) NEW_LINE del maxi [ - 1 ] NEW_LINE j += 1 NEW_LINE DEDENT DEDENT DEDENT def insertMini ( mini , pre_sum ) : NEW_LINE INDENT k = len ( mini ) NEW_LINE for i in range ( k ) : NEW_LINE INDENT if ( pre_sum < mini [ i ] ) : NEW_LINE INDENT mini . insert ( i , pre_sum ) NEW_LINE del mini [ - 1 ] NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT def kMaxOvSubArray ( arr , k ) : NEW_LINE INDENT n = len ( arr ) NEW_LINE pre_sum = prefix_sum ( arr , n ) NEW_LINE mini = [ float ( ' inf ' ) for i in range ( k ) ] NEW_LINE mini [ 0 ] = 0 NEW_LINE maxi = [ - float ( ' inf ' ) for i in range ( k ) ] NEW_LINE cand = [ 0 for i in range ( k ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( k ) : NEW_LINE INDENT cand [ j ] = pre_sum [ i ] - mini [ j ] NEW_LINE DEDENT maxMerge ( maxi , cand ) NEW_LINE insertMini ( mini , pre_sum [ i ] ) NEW_LINE DEDENT for ele in maxi : NEW_LINE INDENT print ( ele , end = ' ▁ ' ) NEW_LINE DEDENT print ( ' ' ) NEW_LINE DEDENT arr1 = [ 4 , - 8 , 9 , - 4 , 1 , - 8 , - 1 , 6 ] NEW_LINE k1 = 4 NEW_LINE kMaxOvSubArray ( arr1 , k1 ) NEW_LINE arr2 = [ - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 ] NEW_LINE k2 = 3 NEW_LINE kMaxOvSubArray ( arr2 , k2 ) NEW_LINE