HomeSoftware DevelopmentMinimal operations to make sum a minimum of M from given two...

Minimal operations to make sum a minimum of M from given two Arrays


// C++ code to implement the strategy
#embody <bits/stdc++.h>
utilizing namespace std;

// dp desk initialized with -1
int dp[501][101][101];

// Recursive Perform to attenuate the
// operations to gather a minimum of sum of M
int clear up(int i, int j, int ok, int A[], int B[], int N)
{

    // Base case
    if (i <= 0) {
        return 0;
    }

    // If reply for present state is
    // already calculated then simply
    // return dp[i][j][k]
    if (dp[i][j][k] != -1)
        return dp[i][j][k];

    // Reply initiallized with zero
    int ans = 1e9;

    // Calling recursive operate for
    // taking j'th aspect of array A[]
    if (j != N)
        ans = min(ans,
                  clear up(i - A[j], j + 1, ok, A, B, N) + 1);

    // Calling recursive operate for
    // taking ok'th aspect of array B[]
    if (ok != N)
        ans = min(ans,
                  clear up(i - B[k], j, ok + 1, A, B, N) + 1);

    // Save and return dp worth
    return dp[i][j][k] = ans;
}

// Perform to attenuate the operations
// to gather a minimum of sum of M
int minOperations(int A[], int B[], int N, int M)
{

    // Filling dp desk with - 1
    memset(dp, -1, sizeof(dp));

    // Minimal operations
    int ans = clear up(M, 0, 0, A, B, N);

    return ans;
}

// Driver Code
int most important()
{

    // Enter 1
    int A[] = { 1, 9, 1, 4, 0, 1 },
        B[] = { 3, 2, 1, 5, 9, 10 };
    int N = sizeof(A) / sizeof(A[0]);
    int M = 12;

    // Perform Name
    cout << minOperations(A, B, N, M) << endl;

    // Enter 2
    int A1[] = { 0, 1, 2, 3, 5 }, B1[] = { 5, 0, 0, 0, 9 };
    int N1 = sizeof(A1) / sizeof(A1[0]);
    int M1 = 6;

    // Perform Name
    cout << minOperations(A1, B1, N1, M1) << endl;
    return 0;
}
RELATED ARTICLES

Most Popular

Recent Comments