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++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; Â
// Function that prints minimum sum // after selecting N elements void minimumSum( int a[], int b[], Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â int c, int n) { Â
    // Initialise the dp array     vector<vector< int > > dp(n,                             vector< int >(2,                                         1e6)); Â
    // Base Case     dp[0][0] = a[0];     dp[0][1] = b[0]; Â
    for ( int i = 1; i < n; i++) { Â
        // Adding the element of array a if         // previous element is also from array a         dp[i][0] = min(dp[i][0],                     dp[i - 1][0] + a[i]); Â
        // Adding the element of array a if         // previous element is from array b         dp[i][0] = min(dp[i][0],                     dp[i - 1][1] + a[i] + c); Â
        // Adding the element of array b if         // previous element is from array a         // with an extra penalty of integer C         dp[i][1] = min(dp[i][1],                     dp[i - 1][0] + b[i] + c); Â
        // Adding the element of array b if         // previous element is also from array b         dp[i][1] = min(dp[i][1],                     dp[i - 1][1] + b[i]);     } Â
    // Print the minimum sum     cout << min(dp[n - 1][0],                 dp[n - 1][1])         << "\n" ; } Â
// Driver Code int main() { Â Â Â Â // Given array arr1[] and arr2[] Â Â Â Â int arr1[] = { 7, 6, 18, 6, 16, Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 18, 1, 17, 17 }; Â
    int arr2[] = { 6, 9, 3, 10, 9,                 1, 10, 1, 5 }; Â
    // Given cost     int C = 2; Â
    int N = sizeof (arr1) / sizeof (arr1[0]); Â
    // Function Call     minimumSum(arr1, arr2, C, N); Â
    return 0; } |
Java
// Java program for the above approach import java.util.*; Â
class GFG{ Â
// Function that prints minimum sum // after selecting N elements static void minimumSum( int a[], int b[],                        int c, int n) {          // Initialise the dp array     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;         }     }          // Base Case     dp[ 0 ][ 0 ] = a[ 0 ];     dp[ 0 ][ 1 ] = b[ 0 ]; Â
    for ( int i = 1 ; i < n; i++)     {                  // Adding the element of array a if         // previous element is also from array a         dp[i][ 0 ] = Math.min(dp[i][ 0 ],                             dp[i - 1 ][ 0 ] + a[i]); Â
        // Adding the element of array a if         // previous element is from array b         dp[i][ 0 ] = Math.min(dp[i][ 0 ],                             dp[i - 1 ][ 1 ] + a[i] + c); Â
        // Adding the element of array b if         // previous element is from array a         // with an extra penalty of integer C         dp[i][ 1 ] = Math.min(dp[i][ 1 ],                             dp[i - 1 ][ 0 ] + b[i] + c); Â
        // Adding the element of array b if         // previous element is also from array b         dp[i][ 1 ] = Math.min(dp[i][ 1 ],                             dp[i - 1 ][ 1 ] + b[i]);     } Â
    // Print the minimum sum     System.out.print(Math.min(dp[n - 1 ][ 0 ],                               dp[n - 1 ][ 1 ]) + "\n" ); } Â
// Driver Code public static void main(String[] args) { Â Â Â Â Â Â Â Â Â // Given array arr1[] and arr2[] Â Â Â Â int arr1[] = { 7 , 6 , 18 , 6 , 16 , Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 18 , 1 , 17 , 17 }; Â
    int arr2[] = { 6 , 9 , 3 , 10 , 9 ,                    1 , 10 , 1 , 5 }; Â
    // Given cost     int C = 2 ; Â
    int N = arr1.length; Â
    // Function call     minimumSum(arr1, arr2, C, N); } } Â
// This code is contributed by gauravrajput1 |
Python3
# Python3 program for the above approach Â
# Function that prints minimum sum # after selecting N elements def minimumSum(a, b, c, n):          # Initialise the dp array     dp = [[ 1e6 for i in range ( 2 )]             for j in range (n)] Â
    # Base Case     dp[ 0 ][ 0 ] = a[ 0 ]     dp[ 0 ][ 1 ] = b[ 0 ] Â
    for i in range ( 1 , n): Â
        # Adding the element of array a if         # previous element is also from array a         dp[i][ 0 ] = min (dp[i][ 0 ],                     dp[i - 1 ][ 0 ] + a[i]) Â
        # Adding the element of array a if         # previous element is from array b         dp[i][ 0 ] = min (dp[i][ 0 ],                     dp[i - 1 ][ 1 ] + a[i] + c) Â
        # Adding the element of array b if         # previous element is from array a         # with an extra penalty of integer C         dp[i][ 1 ] = min (dp[i][ 1 ],                     dp[i - 1 ][ 0 ] + b[i] + c) Â
        # Adding the element of array b if         # previous element is also from array b         dp[i][ 1 ] = min (dp[i][ 1 ],                     dp[i - 1 ][ 1 ] + b[i]) Â
    # Print the minimum sum     print ( min (dp[n - 1 ][ 0 ], dp[n - 1 ][ 1 ])) Â
# Driver code if __name__ = = '__main__' : Â
    # Given array arr[]     arr1 = [ 7 , 6 , 18 , 6 , 16 ,             18 , 1 , 17 , 17 ] Â
    arr2 = [ 6 , 9 , 3 , 10 , 9 ,             1 , 10 , 1 , 5 ] Â
    # Given cost     C = 2 Â
    N = len (arr1) Â
    # Function Call     minimumSum(arr1, arr2, C, N) Â
# This code is contributed by Shivam Singh |
C#
// C# program for the above approach using System; Â
class GFG{ Â
// Function that prints minimum sum // after selecting N elements static void minimumSum( int []a, int []b,                        int c, int n) {          // Initialise the dp array     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;         }     }          // Base Case     dp[0, 0] = a[0];     dp[0, 1] = b[0]; Â
    for ( int i = 1; i < n; i++)     {                  // Adding the element of array a if         // previous element is also from array a         dp[i, 0] = Math.Min(dp[i, 0],                             dp[i - 1, 0] + a[i]); Â
        // Adding the element of array a if         // previous element is from array b         dp[i, 0] = Math.Min(dp[i, 0],                             dp[i - 1, 1] + a[i] + c); Â
        // Adding the element of array b if         // previous element is from array a         // with an extra penalty of integer C         dp[i, 1] = Math.Min(dp[i, 1],                             dp[i - 1, 0] + b[i] + c); Â
        // Adding the element of array b if         // previous element is also from array b         dp[i, 1] = Math.Min(dp[i, 1],                             dp[i - 1, 1] + b[i]);     } Â
    // Print the minimum sum     Console.Write(Math.Min(dp[n - 1, 0],                            dp[n - 1, 1]) + "\n" ); } Â
// Driver Code public static void Main(String[] args) { Â Â Â Â Â Â Â Â Â // Given array arr1[] and arr2[] Â Â Â Â int []arr1 = { 7, 6, 18, 6, 16, Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 18, 1, 17, 17 }; Â
    int []arr2 = { 6, 9, 3, 10, 9,                    1, 10, 1, 5 }; Â
    // Given cost     int C = 2; Â
    int N = arr1.Length; Â
    // Function call     minimumSum(arr1, arr2, C, N); } } Â
// This code is contributed by Rajput-Ji |
Javascript
<script> Â
// JavaScript program for the above approach Â
// Function that prints minimum sum // after selecting N elements function minimumSum(a, b, c, n) {          // Initialise the dp array     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;         }     }          // Base Case     dp[0][0] = a[0];     dp[0][1] = b[0]; Â
    for (let i = 1; i < n; i++)     {                  // Adding the element of array a if         // previous element is also from array a         dp[i][0] = Math.min(dp[i][0],                             dp[i - 1][0] + a[i]); Â
        // Adding the element of array a if         // previous element is from array b         dp[i][0] = Math.min(dp[i][0],                             dp[i - 1][1] + a[i] + c); Â
        // Adding the element of array b if         // previous element is from array a         // with an extra penalty of integer C         dp[i][1] = Math.min(dp[i][1],                             dp[i - 1][0] + b[i] + c); Â
        // Adding the element of array b if         // previous element is also from array b         dp[i][1] = Math.min(dp[i][1],                             dp[i - 1][1] + b[i]);     } Â
    // Print the minimum sum     document.write(Math.min(dp[n - 1][0],                               dp[n - 1][1]) + "<br/>" ); } Â
// Driver Code Â
    // Given array arr1[] and arr2[]     let arr1 = [ 7, 6, 18, 6, 16,                    18, 1, 17, 17 ]; Â
    let arr2 = [ 6, 9, 3, 10, 9,                    1, 10, 1, 5 ]; Â
    // Given cost     let C = 2; Â
    let N = arr1.length; Â
    // Function call     minimumSum(arr1, arr2, C, N); Â
</script> |
49
Â
Time Complexity: O(N), where N is the size of the given array.
Auxiliary Space: O(N), for creating additional dp array.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!