# Maximize sum that can be obtained from two given arrays based on given conditions

• Difficulty Level : Hard
• Last Updated : 13 Sep, 2021

Given two arrays A[] and B[] each of size N, the task is to find the maximum sum that can be obtained based on the following conditions:

• Both A[i] and B[i] cannot be included in the sum ( 0 ≤ i ≤ N – 1 ).
• If B[i] is added to the sum, then B[i – 1] and A[i – 1] cannot be included in the sum ( 0 ≤ i ≤ N – 1 ).

Examples:

Input: A[] = {10, 20, 5}, B[] = {5, 5, 45}
Output: 55
Explanation: The optimal way to maximize the sum is by including A (= 10) and B (= 45) in the sum. Therefore, sum = 10 + 45 = 55.

Input: A[] = {10, 1, 10, 10}, B[] = {5, 50, 1, 5}
Output: 70

Approach: This problem has Optimal substructure and Overlapping subproblems. Therefore, Dynamic Programming can be used to solve the problem.
Follow the steps below to solve the problem:

• Initialize a array, say dp[n], where dp[i] stores the maximum sum if element A[i] is taken into consideration and dp[i] stores the maximum sum if B[i] is taken into consideration.
• Iterate in the range [0, N – 1] using a variable, say i, and perform the following steps:
• If i is equal to 0, then modify the value of dp[i] as A[i] and dp[i] as B[i].
• Otherwise, perform the following operations:
• Modify the value of dp[i] as max(dp[i – 1], dp[i – 1]) + A[i].
• Modify the value of dp[i] as max(dp[i – 1], max(dp[i – 1], max(dp[i – 2], dp[i – 2]) + B[i])).
• After completing the above steps, print the max(dp[N-1], dp[N-1]) as the answer.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `// Function to find the maximum sum``// that can be obtained from two given``// based on the following conditions``int` `MaximumSum(``int` `a[], ``int` `b[], ``int` `n)``{``    ``// Stores the maximum``    ``// sum from 0 to i``    ``int` `dp[n];` `    ``// Initialize the value of``    ``// dp and dp``    ``dp = a;``    ``dp = b;` `    ``// Traverse the array A[] and B[]``    ``for` `(``int` `i = 1; i < n; i++) {``        ``// If A[i] is considered``        ``dp[i] = max(dp[i - 1], dp[i - 1]) + a[i];` `        ``// If B[i] is not considered``        ``dp[i] = max(dp[i - 1], dp[i - 1]);` `        ``// If B[i] is considered``        ``if` `(i - 2 >= 0) {``            ``dp[i] = max(dp[i],``                           ``max(dp[i - 2],``                               ``dp[i - 2])``                               ``+ b[i]);``        ``}``        ``else` `{``            ``// If i = 1, then consider the``            ``// value of  dp[i] as b[i]``            ``dp[i] = max(dp[i], b[i]);``        ``}``    ``}` `    ``// Return maximum Sum``    ``return` `max(dp[n - 1], dp[n - 1]);``}` `// Driver Code``int` `main()``{``    ``// Given Input``    ``int` `A[] = { 10, 1, 10, 10 };``    ``int` `B[] = { 5, 50, 1, 5 };``    ``int` `N = ``sizeof``(A) / ``sizeof``(A);` `    ``// Function Call``    ``cout << MaximumSum(A, B, N);``    ``return` `0;``}`

## Java

 `// Java program for the above approach` `class` `GFG {``    ``// Function to find the maximum sum``    ``// that can be obtained from two given``    ``// based on the following conditions``    ``public` `static` `int` `MaximumSum(``int` `a[], ``int` `b[], ``int` `n) {``        ``// Stores the maximum``        ``// sum from 0 to i``        ``int``[][] dp = ``new` `int``[n][``2``];` `        ``// Initialize the value of``        ``// dp and dp``        ``dp[``0``][``0``] = a[``0``];``        ``dp[``0``][``1``] = b[``0``];` `        ``// Traverse the array A[] and B[]``        ``for` `(``int` `i = ``1``; i < n; i++) {``            ``// If A[i] is considered``            ``dp[i][``0``] = Math.max(dp[i - ``1``][``0``], dp[i - ``1``][``1``]) + a[i];` `            ``// If B[i] is not considered``            ``dp[i][``1``] = Math.max(dp[i - ``1``][``0``], dp[i - ``1``][``1``]);` `            ``// If B[i] is considered``            ``if` `(i - ``2` `>= ``0``) {``                ``dp[i][``1``] = Math.max(dp[i][``1``], Math.max(dp[i - ``2``][``0``], dp[i - ``2``][``1``]) + b[i]);``            ``} ``else``            ``{``              ` `                ``// If i = 1, then consider the``                ``// value of dp[i] as b[i]``                ``dp[i][``1``] = Math.max(dp[i][``1``], b[i]);``            ``}``        ``}` `        ``// Return maximum Sum``        ``return` `Math.max(dp[n - ``1``][``0``], dp[n - ``1``][``1``]);``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String args[]) {``        ``// Given Input``        ``int` `A[] = { ``10``, ``1``, ``10``, ``10` `};``        ``int` `B[] = { ``5``, ``50``, ``1``, ``5` `};``        ``int` `N = A.length;` `        ``// Function Call``        ``System.out.println(MaximumSum(A, B, N));``    ``}``}` `// This  code is contributed by _saurabh_jaiswal.`

## Python3

 `# Python3 program for the above approach` `# Function to find the maximum sum``# that can be obtained from two given``# arrays based on the following conditions``def` `MaximumSum(a, b, n):``  ` `    ``# Stores the maximum``    ``# sum from 0 to i``    ``dp ``=` `[[``-``1` `for` `j ``in` `range``(``2``)]``              ``for` `i ``in` `range``(n)]``    ` `    ``# Initialize the value of``    ``# dp and dp``    ``dp[``0``][``0``] ``=` `a[``0``]``    ``dp[``0``][``1``] ``=` `b[``0``]``    ` `    ``# Traverse the array A[] and B[]``    ``for` `i ``in` `range``(``1``, n):``      ` `        ``# If A[i] is considered``        ``dp[i][``0``] ``=` `max``(dp[i ``-` `1``][``0``],``                       ``dp[i ``-` `1``][``1``]) ``+` `a[i]``        ` `        ``# If B[i] is not considered``        ``dp[i][``1``] ``=` `max``(dp[i ``-` `1``][``0``],``                       ``dp[i ``-` `1``][``1``])``        ` `        ``# If B[i] is considered``        ``if` `(i ``-` `2` `>``=` `0``):``            ``dp[i][``1``] ``=` `max``(dp[i][``1``], ``max``(dp[i ``-` `2``][``0``],``                                         ``dp[i ``-` `2``][``1``]) ``+` `b[i])``        ``else``:``            ` `            ``# If i = 1, then consider the``            ``# value of  dp[i] as b[i]``            ``dp[i][``1``] ``=` `max``(dp[i][``1``], b[i])` `    ``# Return maximum sum``    ``return` `max``(dp[n ``-` `1``][``0``], dp[n ``-` `1``][``1``])` `# Driver code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``# Given input``    ``A ``=` `[ ``10``, ``1``, ``10``, ``10` `]``    ``B ``=` `[ ``5``, ``50``, ``1``, ``5` `]``    ``N ``=` `len``(A)``    ` `    ``# Function call``    ``print``(MaximumSum(A, B, N))``    ` `# This code is contributed by MuskanKalra1`

## C#

 `// C# program for the above approach``using` `System;``using` `System.Collections.Generic;` `class` `GFG{` `// Function to find the maximum sum``// that can be obtained from two given``// based on the following conditions``static` `int` `MaximumSum(``int` `[]a, ``int` `[]b, ``int` `n)``{``  ` `    ``// Stores the maximum``    ``// sum from 0 to i``    ``int` `[,]dp = ``new` `int``[n,2];` `    ``// Initialize the value of``    ``// dp and dp``    ``dp[0,0] = a;``    ``dp[0,1] = b;` `    ``// Traverse the array A[] and B[]``    ``for` `(``int` `i = 1; i < n; i++)``    ``{``      ` `        ``// If A[i] is considered``        ``dp[i,0] = Math.Max(dp[i - 1,0], dp[i - 1,1]) + a[i];` `        ``// If B[i] is not considered``        ``dp[i,1] = Math.Max(dp[i - 1,0], dp[i - 1,1]);` `        ``// If B[i] is considered``        ``if` `(i - 2 >= 0) {``            ``dp[i,1] = Math.Max(dp[i,1],``                           ``Math.Max(dp[i - 2,0],``                               ``dp[i - 2,1])``                               ``+ b[i]);``        ``}``        ``else``        ``{``          ` `            ``// If i = 1, then consider the``            ``// value of  dp[i] as b[i]``            ``dp[i,1] = Math.Max(dp[i,1], b[i]);``        ``}``    ``}` `    ``// Return maximum Sum``    ``return` `Math.Max(dp[n - 1,0], dp[n - 1,1]);``}` `// Driver Code``public` `static` `void` `Main()``{``    ``// Given Input``    ``int` `[]A = { 10, 1, 10, 10 };``    ``int` `[]B = { 5, 50, 1, 5 };``    ``int` `N = A.Length;` `    ``// Function Call``    ``Console.Write(MaximumSum(A, B, N));``}``}` `// This code is contributed by ipg2016107.`

## Javascript

 ``

Output:

`70`

Time Complexity: O(N)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up