Maximum Prefix Sum possible by merging two given arrays | Java 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
import java . util . * ; class GFG { static int maxPresum ( int [ ] a , int [ ] b ) { int X = Math . max ( a [ 0 ] , 0 ) ; for ( int i = 1 ; i < a . length ; 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 . length ; i ++ ) { b [ i ] += b [ i - 1 ] ; Y = Math . max ( Y , b [ i ] ) ; } return X + Y ; } public static void main ( String [ ] args ) { int [ ] A = { 2 , - 1 , 4 , - 5 } ; int [ ] B = { 4 , - 3 , 12 , 4 , - 3 } ; System . out . print ( maxPresum ( A , B ) ) ; } }