Wednesday, December 28, 2022
HomeSoftware DevelopmentDiscover if new Array might be shaped from given two Arrays.

Discover if new Array might be shaped from given two Arrays.


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

// Dp desk initialized with - 1
int dp[200001][3];

// Recusive operate to inform whether or not new
// array might be shaped from given
// two arrays
int recur(int i, int j, int ok, int arr1[], int arr2[],
          int N)
{

    // Base case
    if (i == N) {

        // Return 1 because the creation of
        // new array of measurement N is feasible
        return 1;
    }

    // If present state is precomputed then
    // simply return already computed worth
    if (dp[i][j + 1] != -1)
        return dp[i][j + 1];

    // Reply initialized with contemplating
    // no risk
    int ans = 0;

    // First component to be choosen
    if (j == -1) {

        // Taking first component from arr1[]
        ans = recur(i + 1, 0, ok, arr1, arr2, N);

        // Taking first component from arr2[]
        ans = max(ans, recur(i + 1, 1, ok, arr1, arr2, N));
    }

    // Final elemet was choosen from
    // first array
    else if (j == 0) {

        // If absolutely the distinction with
        // earlier component choosen is much less
        // than equal to Ok then select arr2[i]
        if (abs(arr2[i] - arr1[i - 1]) <= ok)
            ans = recur(i + 1, 1, ok, arr1, arr2, N);

        // If absolutely the distinction with
        // earlier component choosen is lower than
        // equal to Ok then select arr1[i]
        if (abs(arr1[i] - arr1[i - 1]) <= ok)
            ans = max(ans,
                      recur(i + 1, 0, ok, arr1, arr2, N));
    }

    // Final component was choosen from
    // second array
    else {

        // If absolutely the distinction with
        // earlier component choosen is lower than
        // equal to Ok then select arr2[i]
        if (abs(arr2[i] - arr2[i - 1]) <= ok)
            ans = recur(i + 1, 1, ok, arr1, arr2, N);

        // If absolutely the distinction with
        // earlier component choosen is much less
        // than equal to Ok then select arr1[i]
        if (abs(arr1[i] - arr2[i - 1]) <= ok)
            ans = max(ans,
                      recur(i + 1, 0, ok, arr1, arr2, N));
    }

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

// Operate to search out whether or not new array
// might be shaped from the given two
// arrays
void isNewArrayPossible(int arr1[], int arr2[], int N,
                        int Ok)
{

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

    // If recur funtion returns one then
    // it's doable else it's not
    if (recur(0, -1, Ok, arr1, arr2, N))
        cout << "Sure" << endl;
    else
        cout << "No" << endl;
}

// Driver Code
int primary()
{

    // Enter
    int arr1[] = { 9, 8, 3, 7, 2 },
        arr2[] = { 1, 6, 2, 9, 5 };
    int N = sizeof(arr1) / sizeof(arr1[0]);
    int Ok = 4;

    // Operate Name
    isNewArrayPossible(arr1, arr2, N, Ok);

    // Input2
    int arr3[] = { 1, 1, 1, 100 },
        arr4[] = { 1, 2, 3, 100 };
    int N1 = sizeof(arr3) / sizeof(arr3[0]);
    int K1 = 90;

    // Operate name
    isNewArrayPossible(arr3, arr4, N1, K1);
    return 0;
}



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments