Maximum Prefix Sum possible by merging two given arrays | Javascript Program to implement the above approach ; Stores the maximum prefix sum of the array A [ ] ; Traverse the array A [ ] ; Stores the maximum prefix sum of the array B [ ] ; Traverse the array B [ ] ; Driver code
function maxPresum ( a , b ) { let X = Math . max ( a [ 0 ] , 0 ) ; for ( let i = 1 ; i < a . length ; i ++ ) { a [ i ] += a [ i - 1 ] ; X = Math . max ( X , a [ i ] ) ; } let Y = Math . max ( b [ 0 ] , 0 ) ; for ( let i = 1 ; i < b . length ; i ++ ) { b [ i ] += b [ i - 1 ] ; Y = Math . max ( Y , b [ i ] ) ; } return X + Y ; } let A = [ 2 , - 1 , 4 , - 5 ] ; let B = [ 4 , - 3 , 12 , 4 , - 3 ] ; document . write ( maxPresum ( A , B ) ) ;