Maximize sum that can be obtained from two given arrays based on given conditions
Last Updated :
09 Oct, 2023
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[0] (= 10) and B[2] (= 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][2], where dp[i][0] stores the maximum sum if element A[i] is taken into consideration and dp[i][1] 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][0] as A[i] and dp[i][1] as B[i].
- Otherwise, perform the following operations:
- Modify the value of dp[i][0] as max(dp[i – 1][0], dp[i – 1][1]) + A[i].
- Modify the value of dp[i][1] as max(dp[i – 1], max(dp[i – 1][0], max(dp[i – 2][0], dp[i – 2][1]) + B[i])).
- After completing the above steps, print the max(dp[N-1][0], dp[N-1][1]) as the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int MaximumSum( int a[], int b[], int n)
{
int dp[n][2];
dp[0][0] = a[0];
dp[0][1] = b[0];
for ( int i = 1; i < n; i++) {
dp[i][0] = max(dp[i - 1][0], dp[i - 1][1]) + a[i];
dp[i][1] = max(dp[i - 1][0], dp[i - 1][1]);
if (i - 2 >= 0) {
dp[i][1] = max(dp[i][1],
max(dp[i - 2][0],
dp[i - 2][1])
+ b[i]);
}
else {
dp[i][1] = max(dp[i][1], b[i]);
}
}
return max(dp[n - 1][0], dp[n - 1][1]);
}
int main()
{
int A[] = { 10, 1, 10, 10 };
int B[] = { 5, 50, 1, 5 };
int N = sizeof (A) / sizeof (A[0]);
cout << MaximumSum(A, B, N);
return 0;
}
|
Java
class GFG {
public static int MaximumSum( int a[], int b[], int n) {
int [][] dp = new int [n][ 2 ];
dp[ 0 ][ 0 ] = a[ 0 ];
dp[ 0 ][ 1 ] = b[ 0 ];
for ( int i = 1 ; i < n; i++) {
dp[i][ 0 ] = Math.max(dp[i - 1 ][ 0 ], dp[i - 1 ][ 1 ]) + a[i];
dp[i][ 1 ] = Math.max(dp[i - 1 ][ 0 ], dp[i - 1 ][ 1 ]);
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
{
dp[i][ 1 ] = Math.max(dp[i][ 1 ], b[i]);
}
}
return Math.max(dp[n - 1 ][ 0 ], dp[n - 1 ][ 1 ]);
}
public static void main(String args[]) {
int A[] = { 10 , 1 , 10 , 10 };
int B[] = { 5 , 50 , 1 , 5 };
int N = A.length;
System.out.println(MaximumSum(A, B, N));
}
}
|
Python3
def MaximumSum(a, b, n):
dp = [[ - 1 for j in range ( 2 )]
for i in range (n)]
dp[ 0 ][ 0 ] = a[ 0 ]
dp[ 0 ][ 1 ] = b[ 0 ]
for i in range ( 1 , n):
dp[i][ 0 ] = max (dp[i - 1 ][ 0 ],
dp[i - 1 ][ 1 ]) + a[i]
dp[i][ 1 ] = max (dp[i - 1 ][ 0 ],
dp[i - 1 ][ 1 ])
if (i - 2 > = 0 ):
dp[i][ 1 ] = max (dp[i][ 1 ], max (dp[i - 2 ][ 0 ],
dp[i - 2 ][ 1 ]) + b[i])
else :
dp[i][ 1 ] = max (dp[i][ 1 ], b[i])
return max (dp[n - 1 ][ 0 ], dp[n - 1 ][ 1 ])
if __name__ = = '__main__' :
A = [ 10 , 1 , 10 , 10 ]
B = [ 5 , 50 , 1 , 5 ]
N = len (A)
print (MaximumSum(A, B, N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int MaximumSum( int []a, int []b, int n)
{
int [,]dp = new int [n,2];
dp[0,0] = a[0];
dp[0,1] = b[0];
for ( int i = 1; i < n; i++)
{
dp[i,0] = Math.Max(dp[i - 1,0], dp[i - 1,1]) + a[i];
dp[i,1] = Math.Max(dp[i - 1,0], dp[i - 1,1]);
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
{
dp[i,1] = Math.Max(dp[i,1], b[i]);
}
}
return Math.Max(dp[n - 1,0], dp[n - 1,1]);
}
public static void Main()
{
int []A = { 10, 1, 10, 10 };
int []B = { 5, 50, 1, 5 };
int N = A.Length;
Console.Write(MaximumSum(A, B, N));
}
}
|
Javascript
<script>
function MaximumSum(a, b, n)
{
let dp = new Array(n).fill(0).map(
() => new Array(2));
dp[0][0] = a[0];
dp[0][1] = b[0];
for (let i = 1; i < n; i++)
{
dp[i][0] = Math.max(dp[i - 1][0],
dp[i - 1][1]) + a[i];
dp[i][1] = Math.max(dp[i - 1][0],
dp[i - 1][1]);
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
{
dp[i][1] = Math.max(dp[i][1], b[i]);
}
}
return Math.max(dp[n - 1][0], dp[n - 1][1]);
}
let A = [ 10, 1, 10, 10 ];
let B = [ 5, 50, 1, 5 ];
let N = A.length;
document.write(MaximumSum(A, B, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Efficient Approach – Space optimization: In the previous approach, the current value dp[i] is only dependent upon the just previous values i.e., dp[i – 1]. So to optimize the space we can keep track of previous and current values with the help of variables prev_max_a, prev_max_b and curr_max_a, curr_max_b which will reduce the space complexity from O(N) to O(1). Below are the steps:
- Create variables prev_max_a and prev_max_b to keep track of previous values of DP.
- Initialize base case prev_max_a = a[0] , prev_max_b = b[0].
- Create a variable curr_max_a and curr_max_b to store the current value.
- Iterate over the subproblem using loop and update curr_max_a and curr_max_b and after every iteration, update prev_max_a and prev_max_b for further iterations.
- After the above steps, print the maximum of curr_max_a and curr_max_b.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int MaximumSum( int a[], int b[], int n)
{
int prev_max_a = a[0], curr_max_a = 0;
int prev_max_b = b[0], curr_max_b = 0;
for ( int i = 1; i < n; i++) {
curr_max_a = max(prev_max_a, prev_max_b) + a[i];
curr_max_b = max(prev_max_a, prev_max_b);
if (i - 2 >= 0) {
curr_max_b = max(curr_max_b, max(prev_max_a, prev_max_b) + b[i]);
}
else {
curr_max_b = max(curr_max_b, b[i]);
}
prev_max_a = curr_max_a;
prev_max_b = curr_max_b;
}
return max(curr_max_a, curr_max_b);
}
int main()
{
int A[] = { 10, 1, 10, 10 };
int B[] = { 5, 50, 1, 5 };
int N = sizeof (A) / sizeof (A[0]);
cout << MaximumSum(A, B, N);
return 0;
}
|
Java
import java.util.*;
public class MaximumSumTwoArrays {
public static int maximumSum( int [] a, int [] b, int n) {
int prevMaxA = a[ 0 ];
int currMaxA = 0 ;
int prevMaxB = b[ 0 ];
int currMaxB = 0 ;
for ( int i = 1 ; i < n; i++) {
currMaxA = Math.max(prevMaxA, prevMaxB) + a[i];
currMaxB = Math.max(prevMaxA, prevMaxB);
if (i - 2 >= 0 ) {
currMaxB = Math.max(currMaxB, Math.max(prevMaxA, prevMaxB) + b[i]);
} else {
currMaxB = Math.max(currMaxB, b[i]);
}
prevMaxA = currMaxA;
prevMaxB = currMaxB;
}
return Math.max(currMaxA, currMaxB);
}
public static void main(String[] args) {
int [] A = { 10 , 1 , 10 , 10 };
int [] B = { 5 , 50 , 1 , 5 };
int N = A.length;
System.out.println(maximumSum(A, B, N));
}
}
|
Python
def MaximumSum(a, b, n):
prev_max_a = a[ 0 ]
curr_max_a = 0
prev_max_b = b[ 0 ]
curr_max_b = 0
for i in range ( 1 , n):
curr_max_a = max (prev_max_a, prev_max_b) + a[i]
curr_max_b = max (prev_max_a, prev_max_b)
if i - 2 > = 0 :
curr_max_b = max (curr_max_b, max (prev_max_a, prev_max_b) + b[i])
else :
curr_max_b = max (curr_max_b, b[i])
prev_max_a = curr_max_a
prev_max_b = curr_max_b
return max (curr_max_a, curr_max_b)
if __name__ = = '__main__' :
A = [ 10 , 1 , 10 , 10 ]
B = [ 5 , 50 , 1 , 5 ]
N = len (A)
print (MaximumSum(A, B, N))
|
C#
using System;
class Program {
static int MaximumSum( int [] a, int [] b, int n)
{
int prevMaxA = a[0], currMaxA = 0;
int prevMaxB = b[0], currMaxB = 0;
for ( int i = 1; i < n; i++) {
currMaxA = Math.Max(prevMaxA, prevMaxB) + a[i];
currMaxB = Math.Max(prevMaxA, prevMaxB);
if (i - 2 >= 0) {
currMaxB = Math.Max(
currMaxB,
Math.Max(prevMaxA, prevMaxB) + b[i]);
}
else {
currMaxB = Math.Max(currMaxB, b[i]);
}
prevMaxA = currMaxA;
prevMaxB = currMaxB;
}
return Math.Max(currMaxA, currMaxB);
}
static void Main()
{
int [] A = { 10, 1, 10, 10 };
int [] B = { 5, 50, 1, 5 };
int N = A.Length;
Console.WriteLine(MaximumSum(A, B, N));
}
}
|
Javascript
function MaximumSum(a, b, n) {
let prev_max_a = a[0];
let curr_max_a = 0;
let prev_max_b = b[0];
let curr_max_b = 0;
for (let i = 1; i < n; i++) {
curr_max_a = Math.max(prev_max_a, prev_max_b) + a[i];
curr_max_b = Math.max(prev_max_a, prev_max_b);
if (i - 2 >= 0) {
curr_max_b = Math.max(curr_max_b, Math.max(prev_max_a, prev_max_b) + b[i]);
}
else {
curr_max_b = Math.max(curr_max_b, b[i]);
}
prev_max_a = curr_max_a;
prev_max_b = curr_max_b;
}
return Math.max(curr_max_a, curr_max_b);
}
const A = [10, 1, 10, 10];
const B = [5, 50, 1, 5];
const N = A.length;
console.log(MaximumSum(A, B, N));
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...