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 ; Given arrays and respective lengths ; Calculate maximum subarray sum
function maxSum ( arr , brr , N , K ) { let crr = [ ] ; for ( let i = 0 ; i < N ; i ++ ) { crr . push ( arr [ i ] ) ; } for ( let i = 0 ; i < K ; i ++ ) { crr . push ( brr [ i ] ) ; } crr . sort ( function ( a , b ) { return a - b } ) ; crr . reverse ( ) ; let sum = 0 ; for ( let i = 0 ; i < N ; i ++ ) { if ( crr [ i ] > 0 ) { sum += crr [ i ] ; } else { break ; } } document . write ( sum ) ; } let arr = [ 7 , 2 , - 1 , 4 , 5 ] ; let N = arr . length ; let brr = [ 1 , 2 , 3 , 2 ] ; let K = brr . length ; maxSum ( arr , brr , N , K ) ;