Maximum sum combination from two arrays
Given two arrays arr1[] and arr2[] each of size N. The task is to choose some elements from both the arrays such that no two elements have the same index and no two consecutive numbers can be selected from a single array. Find the maximum sum possible of above-chosen numbers.
Examples:
Input : arr1[] = {9, 3, 5, 7, 3}, arr2[] = {5, 8, 1, 4, 5}
Output : 29
Select first, third and fivth element from the first array.
Select the second and fourth element from the second array.Input : arr1[] = {1, 2, 9}, arr2[] = {10, 1, 1}
Output : 19
Select last element from the first array and first element from the second array.
Approach :
This problem is based on dynamic programming.
.
Recursion realtions are :
dp(i, 1)=max(dp (i – 1, 2) + arr(i, 1), dp(i – 1, 3) + arr(i, 1), arr(i, 1) );
dp(i, 2)=max(dp(i – 1, 1) + arr(i, 2 ), dp(i – 1, 3) + arr (i, 2), arr(i, 2));
dp(i, 3)=max(dp(i- 1, 1), dp( i-1, 2) ).
We don’t actually need dp( i, 3), if we update dp(i, 1) as max(dp(i, 1), dp(i-1, 1)) and dp(i, 2) as max(dp(i, 2), dp(i-1, 2)).
Thus, dp(i, j) is the maximum total sum of the elements that are selected if the last element was taken from the position (i-1, 1) or less. The same with dp(i, 2). Therefore the answer to the above problem is max(dp(n, 1), dp(n, 2)).
Below is the implementation of the above approach :
C++
// CPP program to maximum sum // combination from two arrays #include <bits/stdc++.h> using namespace std; // Function to maximum sum // combination from two arrays int Max_Sum(int arr1[], int arr2[], int n) { // To store dp value int dp[n][2]; // For loop to calculate the value of dp for (int i = 0; i < n; i++) { if(i==0) { dp[i][0] = arr1[i]; dp[i][1] = arr2[i]; continue; } dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] + arr1[i]); dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + arr2[i]); } // Return the required answer return max(dp[n-1][0], dp[n-1][1]); } // Driver code int main() { int arr1[] = {9, 3, 5, 7, 3}; int arr2[] = {5, 8, 1, 4, 5}; int n = sizeof(arr1) / sizeof(arr1[0]); // Function call cout << Max_Sum(arr1, arr2, n); return 0; } |
Java
// Java program to maximum sum // combination from two arrays class GFG { // Function to maximum sum // combination from two arrays static int Max_Sum(int arr1[], int arr2[], int n) { // To store dp value int [][]dp = new int[n][2]; // For loop to calculate the value of dp for (int i = 0; i < n; i++) { if(i == 0) { dp[i][0] = arr1[i]; dp[i][1] = arr2[i]; continue; } dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + arr1[i]); dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] + arr2[i]); } // Return the required answer return Math.max(dp[n - 1][0], dp[n - 1][1]); } // Driver code public static void main(String[] args) { int arr1[] = {9, 3, 5, 7, 3}; int arr2[] = {5, 8, 1, 4, 5}; int n = arr1.length; // Function call System.out.println(Max_Sum(arr1, arr2, n)); } } // This code is contributed // by PrinciRaj1992 |
C#
// C# program to maximum sum
// combination from two arrays
using System;
class GFG
{
// Function to maximum sum
// combination from two arrays
static int Max_Sum(int []arr1,
int []arr2, int n)
{
// To store dp value
int [,]dp = new int[n, 2];
// For loop to calculate the value of dp
for (int i = 0; i < n; i++)
{
if(i == 0)
{
dp[i, 0] = arr1[i];
dp[i, 1] = arr2[i];
continue;
}
dp[i, 0] = Math.Max(dp[i - 1, 0],
dp[i - 1, 1] + arr1[i]);
dp[i, 1] = Math.Max(dp[i - 1, 1],
dp[i - 1, 0] + arr2[i]);
}
// Return the required answer
return Math.Max(dp[n - 1, 0],
dp[n - 1, 1]);
}
// Driver code
public static void Main()
{
int []arr1 = {9, 3, 5, 7, 3};
int []arr2 = {5, 8, 1, 4, 5};
int n = arr1.Length;
// Function call
Console.WriteLine(Max_Sum(arr1, arr2, n));
}
}
// This code is contributed
// by anuj_67..
[tabbyending]
29
Time Complexity: O(N)
Recommended Posts:
- Split the given array into K sub-arrays such that maximum sum of all sub arrays is minimum
- Find all unique pairs of maximum and second maximum elements over all sub-arrays in O(NlogN)
- Maximum OR sum of sub-arrays of two different arrays
- Maximum Sum of Products of Two Arrays
- Maximum Sum Path in Two Arrays
- K maximum sum combinations from two arrays
- Find Sum of pair from two arrays with maximum sum
- Maximum sum of increasing order elements from n arrays
- Maximum sum by picking elements from two arrays in order
- Compute maximum of the function efficiently over all sub-arrays
- K maximum sums of non-overlapping contiguous sub-arrays
- Find Maximum dot product of two arrays with insertion of 0's
- Maximize the maximum among minimum of K consecutive sub-arrays
- Maximum array from two given arrays keeping order same
- Maximum absolute difference between sum of two contiguous sub-arrays
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



