Given two arrays A[] and B[] of N positive integers and a cost C. We can choose any one element from each index of the given arrays i.e., for any index i we can choose only element A[i] or B[i]. The task is to find the minimum total sum of selecting N elements from the given two arrays and if we are selecting any element from A[] to B[] or vice-versa in the next iteration then the cost C is also added to the sum.
Note: Choose element in increasing order of index i.e., 0 ? i < N.
Examples:
Input: N = 9, A[] = {7, 6, 18, 6, 16, 18, 1, 17, 17}, B[] = {6, 9, 3, 10, 9, 1, 10, 1, 5}, C = 2
Output: 49
Explanation:
On taking the 1st element from array A, sum = 7
On taking the 2nd element from array A, sum = 7 + 6 = 13
On taking the 3rd element from array B, as we are entering from array A to array B, sum = 13 + 3 + 2 = 18
On taking the 4th element from array A, as we are entering from array B to array A, sum = 18 + 6 + 2 = 26
On taking the 5th element from array B, as we are entering from array A to array B, sum = 26 + 9 + 2 = 37
On taking the 6th element form array B, sum = 37 + 1 = 38
On taking the 7th element from array A, as we are entering from array B to array A, sum = 38 + 1 + 2 = 41
On taking the 8th element form array B, as we are entering from array A to array B, sum = 41 + 1 + 2 = 44
On taking the 9th element from array B, sum = 44 + 5 = 49.
Input: N = 9, A = {3, 2, 3, 1, 3, 3, 1, 4, 1}, B = {1, 2, 3, 4, 4, 1, 2, 1, 3}, C = 1
Output: 18
Explanation:
On taking the 1st element from array B, sum = 1
On taking the 2nd element from array A, sum = 1 + 2 = 3
On taking the 3rd element from array A, sum = 3 + 3 = 6
On taking the 4th element from array A, sum = 6 + 1 = 7
On taking the 5th element from array A, sum = 7 + 3 = 10
On taking the 6th element form array B, as we are entering from array A to array B, sum = 10 + 1 + 1 = 12
On taking the 7th element from array A, as we are entering from array B to array A, sum = 12 + 1 + 1 = 14
On taking the 8th element form array B, as we are entering from array A to array B, sum = 14 + 1 + 1 = 16
On taking the 9th element from array A, as we are entering from array B to array A, sum = 16 + 1 + 1 = 18.
Approach: We will use Dynamic Programming to solve this problem. Below are the steps:
- Create a 2D array dp[][] of N rows and two columns and initialize all elements of dp to infinity.
- There can be 4 possible cases of adding the elements from both the arrays:
- Adding an element from array a[] when the previously added element is from array a[].
- Adding an element from array a[] when the previously added element is from array b[]. In this case there is a penalty of adding the integer C with the result.
- Adding an element from array b[] when the previously added element is from array b[].
- Adding an element from array b[] when the previously added element is from array a[]. In this case there is a penalty of adding the integer C with the result.
- Update the dp array each time with the minimum value of the above four conditions.
- The minimum of dp[n-1][0] and dp[n-1][1] is the total minimum sum of selecting N elements.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void minimumSum( int a[], int b[],
int c, int n)
{
vector<vector< int > > dp(n,
vector< int >(2,
1e6));
dp[0][0] = a[0];
dp[0][1] = b[0];
for ( int i = 1; i < n; i++) {
dp[i][0] = min(dp[i][0],
dp[i - 1][0] + a[i]);
dp[i][0] = min(dp[i][0],
dp[i - 1][1] + a[i] + c);
dp[i][1] = min(dp[i][1],
dp[i - 1][0] + b[i] + c);
dp[i][1] = min(dp[i][1],
dp[i - 1][1] + b[i]);
}
cout << min(dp[n - 1][0],
dp[n - 1][1])
<< "\n" ;
}
int main()
{
int arr1[] = { 7, 6, 18, 6, 16,
18, 1, 17, 17 };
int arr2[] = { 6, 9, 3, 10, 9,
1, 10, 1, 5 };
int C = 2;
int N = sizeof (arr1) / sizeof (arr1[0]);
minimumSum(arr1, arr2, C, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void minimumSum( int a[], int b[],
int c, int n)
{
int [][]dp = new int [n][ 2 ];
for ( int i = 0 ; i < n; i++)
{
for ( int j = 0 ; j < 2 ; j++)
{
dp[i][j] = ( int ) 1e6;
}
}
dp[ 0 ][ 0 ] = a[ 0 ];
dp[ 0 ][ 1 ] = b[ 0 ];
for ( int i = 1 ; i < n; i++)
{
dp[i][ 0 ] = Math.min(dp[i][ 0 ],
dp[i - 1 ][ 0 ] + a[i]);
dp[i][ 0 ] = Math.min(dp[i][ 0 ],
dp[i - 1 ][ 1 ] + a[i] + c);
dp[i][ 1 ] = Math.min(dp[i][ 1 ],
dp[i - 1 ][ 0 ] + b[i] + c);
dp[i][ 1 ] = Math.min(dp[i][ 1 ],
dp[i - 1 ][ 1 ] + b[i]);
}
System.out.print(Math.min(dp[n - 1 ][ 0 ],
dp[n - 1 ][ 1 ]) + "\n" );
}
public static void main(String[] args)
{
int arr1[] = { 7 , 6 , 18 , 6 , 16 ,
18 , 1 , 17 , 17 };
int arr2[] = { 6 , 9 , 3 , 10 , 9 ,
1 , 10 , 1 , 5 };
int C = 2 ;
int N = arr1.length;
minimumSum(arr1, arr2, C, N);
}
}
|
Python3
def minimumSum(a, b, c, n):
dp = [[ 1e6 for i in range ( 2 )]
for j in range (n)]
dp[ 0 ][ 0 ] = a[ 0 ]
dp[ 0 ][ 1 ] = b[ 0 ]
for i in range ( 1 , n):
dp[i][ 0 ] = min (dp[i][ 0 ],
dp[i - 1 ][ 0 ] + a[i])
dp[i][ 0 ] = min (dp[i][ 0 ],
dp[i - 1 ][ 1 ] + a[i] + c)
dp[i][ 1 ] = min (dp[i][ 1 ],
dp[i - 1 ][ 0 ] + b[i] + c)
dp[i][ 1 ] = min (dp[i][ 1 ],
dp[i - 1 ][ 1 ] + b[i])
print ( min (dp[n - 1 ][ 0 ], dp[n - 1 ][ 1 ]))
if __name__ = = '__main__' :
arr1 = [ 7 , 6 , 18 , 6 , 16 ,
18 , 1 , 17 , 17 ]
arr2 = [ 6 , 9 , 3 , 10 , 9 ,
1 , 10 , 1 , 5 ]
C = 2
N = len (arr1)
minimumSum(arr1, arr2, C, N)
|
C#
using System;
class GFG{
static void minimumSum( int []a, int []b,
int c, int n)
{
int [,]dp = new int [n, 2];
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < 2; j++)
{
dp[i, j] = ( int )1e6;
}
}
dp[0, 0] = a[0];
dp[0, 1] = b[0];
for ( int i = 1; i < n; i++)
{
dp[i, 0] = Math.Min(dp[i, 0],
dp[i - 1, 0] + a[i]);
dp[i, 0] = Math.Min(dp[i, 0],
dp[i - 1, 1] + a[i] + c);
dp[i, 1] = Math.Min(dp[i, 1],
dp[i - 1, 0] + b[i] + c);
dp[i, 1] = Math.Min(dp[i, 1],
dp[i - 1, 1] + b[i]);
}
Console.Write(Math.Min(dp[n - 1, 0],
dp[n - 1, 1]) + "\n" );
}
public static void Main(String[] args)
{
int []arr1 = { 7, 6, 18, 6, 16,
18, 1, 17, 17 };
int []arr2 = { 6, 9, 3, 10, 9,
1, 10, 1, 5 };
int C = 2;
int N = arr1.Length;
minimumSum(arr1, arr2, C, N);
}
}
|
Javascript
<script>
function minimumSum(a, b, c, n)
{
let dp = new Array(n);
for ( var i = 0; i < dp.length; i++) {
dp[i] = new Array(2);
}
for (let i = 0; i < n; i++)
{
for (let j = 0; j < 2; j++)
{
dp[i][j] = 1e6;
}
}
dp[0][0] = a[0];
dp[0][1] = b[0];
for (let i = 1; i < n; i++)
{
dp[i][0] = Math.min(dp[i][0],
dp[i - 1][0] + a[i]);
dp[i][0] = Math.min(dp[i][0],
dp[i - 1][1] + a[i] + c);
dp[i][1] = Math.min(dp[i][1],
dp[i - 1][0] + b[i] + c);
dp[i][1] = Math.min(dp[i][1],
dp[i - 1][1] + b[i]);
}
document.write(Math.min(dp[n - 1][0],
dp[n - 1][1]) + "<br/>" );
}
let arr1 = [ 7, 6, 18, 6, 16,
18, 1, 17, 17 ];
let arr2 = [ 6, 9, 3, 10, 9,
1, 10, 1, 5 ];
let C = 2;
let N = arr1.length;
minimumSum(arr1, arr2, C, N);
</script>
|
Time Complexity: O(N), where N is the size of the given array.
Auxiliary Space: O(N), for creating additional dp array.