Maximum Prefix Sum possible by merging two given arrays | C # 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
using System ; using System . Collections . Generic ; class GFG { static int maxPresum ( List < int > a , List < int > b ) { int X = Math . Max ( a [ 0 ] , 0 ) ; for ( int i = 1 ; i < a . Count ; i ++ ) { a [ i ] += a [ i - 1 ] ; X = Math . Max ( X , a [ i ] ) ; } int Y = Math . Max ( b [ 0 ] , 0 ) ; for ( int i = 1 ; i < b . Count ; i ++ ) { b [ i ] += b [ i - 1 ] ; Y = Math . Max ( Y , b [ i ] ) ; } return X + Y ; } static void Main ( ) { List < int > A = new List < int > ( new int [ ] { 2 , - 1 , 4 , - 5 } ) ; List < int > B = new List < int > ( new int [ ] { 4 , - 3 , 12 , 4 , - 3 } ) ; Console . WriteLine ( maxPresum ( A , B ) ) ; } }