Sunday, January 29, 2023
HomeSoftware DevelopmentMinimal operations to make sum at the least M from given two...

# Minimal operations to make sum at the least M from given two Arrays

```// C++ code to implement the method
#embrace <bits/stdc++.h>
utilizing namespace std;

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

// Recursive Operate to reduce the
// operations to gather at the least sum of M
int remedy(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
// return dp[i][j][k]
if (dp[i][j][k] != -1)
return dp[i][j][k];

int ans = 1e9;

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

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

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

// Operate to reduce the operations
// to gather at the least 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 = remedy(M, 0, 0, A, B, N);

return ans;
}

// Driver Code
int essential()
{

// 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;

// Operate 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;

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