Geeksforgeeks

Timer is Running

Associated Course(s):
Interview Preparation
Amazon SDE Test Series

Given two sorted arrays A and B, such that the arrays may have some common elements. Find the sum of the maximum sum path to reach from the beginning of any array to end of any of the two arrays. We can switch from one array to another array only at the common elements.

**Example 1:**

**Input:
**M = 5, N = 4
A[] = {2,3,7,10,12}
B[] = {1,5,7,8}
**Output: **35**
Explanation: **The path will be 1+5+7+10+12
= 35.

**Example 2:**

**Input:
**M = 3, N = 3
A[] = {1,2,4}
B[] = {1,2,7}
**Output: **10**
Explanation: **The path will be 1+2+7=10.

**Your Task:**

Complete the function **max_path_sum()** that takes the two arrays A and B along with their sizes M and N as input parameters. It returns the sum of the maximum sum path.

**Expected Time Complexity: **O(M + N)

**Expected Auxiliary Space:** O(1)

**Constraints:**

1 <= M,N <= 10^{5}

1 <= A[i], B[i] <= 10^{6}

Author: Shubham Joshi 1

If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.

jainshubham52535 | 529 |

Pathak1899 | 436 |

yashdip123 | 380 |

kirtidee18 | 377 |

jots62 | 351 |

kirtidee18 | 1057 |

ykaushik | 752 |

aggarwalvatsal5 | 741 |

harbilassingh | 576 |

pmr_3689 | 570 |

blackshadows | 5456 |

Ibrahim Nash | 5450 |

akhayrutdinov | 5111 |

mb1973 | 4976 |

Quandray | 4827 |

Login to report an issue on this page.