Skip to content

Commit

Permalink
Merge pull request #188 from pradip2004/Max_sum_path_in_two_arrays/Cpp
Browse files Browse the repository at this point in the history
Added New Medium-Level Question: 'Max Sum Path in Two Arrays'
  • Loading branch information
JenilGajjar20 authored Oct 18, 2024
2 parents 5c9ab77 + accf873 commit 5baa6b4
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
40 changes: 40 additions & 0 deletions Cpp/Max-sum-path-in-two-arrays/problem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Maximum Sum Path in Two Sorted Arrays

## Problem Statement

Given two sorted arrays `arr1` and `arr2` of distinct integers, some elements may be common between the two arrays. We need to find the maximum sum of a path from the beginning of either array to the end of any array. The twist is that you can switch from one array to the other array only at common elements, and when you switch, the common element should be included only once in the result.

### Example 1:

**Input:**
`arr1 = [2, 3, 7, 10, 12]`
`arr2 = [1, 5, 7, 8]`

**Output:**
`35`

**Explanation:**
The optimal path is:
1 + 5 + 7 (switch from `arr2` to `arr1` at 7) + 10 + 12 = **35**

### Example 2:

**Input:**
`arr1 = [1, 2, 3]`
`arr2 = [3, 4, 5]`

**Output:**
`15`

**Explanation:**
The optimal path is:
1 + 2 + 3 (switch from `arr1` to `arr2` at 3) + 4 + 5 = **15**

---

## Constraints

- `1 <= arr1.size(), arr2.size() <= 10^4`
- `1 <= arr1[i], arr2[i] <= 10^5`

---
75 changes: 75 additions & 0 deletions Cpp/Max-sum-path-in-two-arrays/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include <iostream>
using namespace std;

class Solution
{
public:
/* Function to find the maximum path sum between two arrays */
/*
l1: The length of array A.
l2: The length of array B.
*/
int max_path_sum(int A[], int B[], int l1, int l2)
{
int i = 0, j = 0; // Pointers for both arrays
int path = 0; // Final maximum path sum
int sum1 = 0, sum2 = 0; // Accumulators for array sums

// Traverse both arrays until one is fully traversed
while (i < l1 && j < l2)
{
if (A[i] < B[j])
{ // Add to sum1 if element in A is smaller
sum1 += A[i];
i++;
}
else if (A[i] > B[j])
{ // Add to sum2 if element in B is smaller
sum2 += B[j];
j++;
}
else
{ // Common element found
path += max(sum1, sum2) + A[i]; // Add max sum + common element
i++;
j++;
sum1 = sum2 = 0; // Reset sums
}
}

// Add remaining elements in A
while (i < l1)
{
sum1 += A[i];
i++;
}

// Add remaining elements in B
while (j < l2)
{
sum2 += B[j];
j++;
}

// Add the maximum of the remaining sums
path += max(sum1, sum2);
return path;
}
};

int main()
{
Solution sol;
int A[] = {2, 3, 7, 10, 12};
int B[] = {1, 5, 7, 8};
int l1 = sizeof(A) / sizeof(A[0]);
int l2 = sizeof(B) / sizeof(B[0]);

cout << "Maximum Path Sum: " << sol.max_path_sum(A, B, l1, l2) << endl;
return 0;
}

/*
Time Complexity: O(l1 + l2)
Space Complexity: O(1)
*/

0 comments on commit 5baa6b4

Please sign in to comment.