Maximize maximum possible subarray sum of an array by swapping with elements from another array | Function to find the maximum subarray sum possible by swapping elements from array arr [ ] with that from array brr [ ] ; Stores elements from the arrays arr [ ] and brr [ ] ; Store elements of array arr [ ] and brr [ ] in the vector crr ; Sort the vector crr in descending order ; Stores maximum sum ; Calculate the sum till the last index in crr [ ] which is less than N which contains a positive element ; Print the sum ; Driver code ; Given arrays and respective lengths ; Calculate maximum subarray sum
def maxSum ( arr , brr , N , K ) : NEW_LINE INDENT crr = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT crr . append ( arr [ i ] ) NEW_LINE DEDENT for i in range ( K ) : NEW_LINE INDENT crr . append ( brr [ i ] ) NEW_LINE DEDENT crr = sorted ( crr ) [ : : - 1 ] NEW_LINE sum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( crr [ i ] > 0 ) : NEW_LINE INDENT sum += crr [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT print ( sum ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 7 , 2 , - 1 , 4 , 5 ] NEW_LINE N = len ( arr ) NEW_LINE brr = [ 1 , 2 , 3 , 2 ] NEW_LINE K = len ( brr ) NEW_LINE maxSum ( arr , brr , N , K ) NEW_LINE DEDENT