Trends

Check for formation of X with given operations

Trending 1 year ago
beritaja.com

Given array A[] of size N, array B[] of size M, and integer X, the task for this problem is to show whether number X is imaginable to shape aliases not if immoderate number from array A[] is added to immoderate number of times stepwise and successful each measurement sum of number should not beryllium adjacent to immoderate number from B[]. Print “Yes” if imaginable other “No”

Examples:

Input: A[] = {3, 4, 5}, B[] = {4, 5, 6, 8}, X = 15
Output: Yes
Explanation: Initially, we person a existent worth of zero let’s opportunity curVal = 0

  • Step 1 : Add A[0] = 3 successful existent value, curVal = 3 and B[] does not person 3 truthful move is valid
  • Step 2 : Add A[1] = 4 successful existent value, curVal = 7 and B[] does not person 7 truthful move is valid
  • Step 3 : Add A[2] = 5 successful existent value, curVal = 12 and B[] does not person 12 truthful move is valid
  • Step 4 : Add A[0] = 3 successful existent value, curVal = 15  and B[] does not person 15 truthful move is valid

So 15 is imaginable to people “Yes”.

Input: A[] = {2, 3, 4, 5}, B[] = {3, 4, 5, 6}, X = 8
Output:  No 

Naive approach: The basal measurement to lick nan problem is arsenic follows:

The basal measurement to lick this problem is to make each imaginable combinations by utilizing a recursive approach.

Time Complexity: O(XN)
Auxiliary Space: O(1)

Efficient Approach/Memoization:  The supra attack tin beryllium optimized based connected nan pursuing idea:

Dynamic programming tin beryllium utilized to lick this problem:

  • dp[i] represents whether i is imaginable aliases not from immoderate combination.
  • It tin beryllium observed that nan recursive usability is called exponential times. That intends that immoderate states are called repeatedly. 
  • So nan thought is to shop nan worth of each state. This tin beryllium done by storing nan worth of a authorities and whenever nan usability is called, returning nan stored worth without computing again.

 Recursion Tree of nan first example.

  • Green is simply a valid move whereas reddish is an invalid move.
  • This is observed that astatine 11 and 12 aforesaid sub-tree is repeated arsenic that portion is already calculated simply return that portion without calculating again (that is simply returning calculated values of repeated states).

Recursion Tree of nan first example

Follow nan steps beneath to lick nan problem:

  • Create HashMap[] and people each values from B[] arsenic 1 in HashMap[].
  • Create a recursive usability that takes 1 parameter i which represents nan existent value.
  • Call nan recursive usability for choosing each numbers from A[].
  • Base lawsuit if nan existent worth is X return 1.
  • Create a 1d array of dp[100001] initially filled pinch -1.
  • If nan reply for a peculiar authorities is computed past prevention it successful dp[i].
  • If nan reply for a peculiar authorities is already computed past conscionable return dp[i].

Below is nan implementation of nan supra approach:

C++

// C++ codification to instrumentality nan approach #include <bits/stdc++.h> using namespace std; // DP array initialized pinch -1 int dp[100001]; // Recursive Function to show whether X // is imaginable aliases not int recur(int i, int A[], int N, vector<int>& HashMap, int X) { // Base case if (i == X) { return 1; } // Initializing ans to 0, mendacious value int ans = 0; // Calling recursive usability for all // imaginable moves for (int j = 0; j < N; j++) { // Call function if (i + A[j] <= X and !HashMap[i + A[j]]) ans |= recur(i + A[j], A, N, HashMap, X); } // Save and return dp value return dp[i] = ans; } // Function to show whether X is // imaginable aliases not void isPossible(int A[], int B[], int N, int M, int X) { // Initializing dp array pinch - 1 memset(dp, -1, sizeof(dp)); // Creating HashMap vector<int> HashMap(100001, 0); // Marking each nan B's which are // coming successful HashMap[] for (int one = 0; one < M; i++) { // B[i] exists HashMap[B[i]] = 1; } // Calling recursive usability for // uncovering answer int ans = recur(0, A, N, HashMap, X); // If ans is 1 then if (ans) cout << "Yes" << endl; // Else else cout << "No" << endl; } // Driver Code int main() { // Input 1 int A1[] = { 3, 4, 5 }, B1[] = { 4, 5, 6, 8 }, X1 = 15; int N1 = sizeof(A1) / sizeof(A1[0]); int M1 = sizeof(B1) / sizeof(B1[0]); // Function Call isPossible(A1, B1, N1, M1, X1); // Input 2 int A2[] = { 2, 3, 4, 5 }, B2[] = { 3, 4, 5, 6 }, X2 = 8; int N2 = sizeof(A2) / sizeof(A2[0]); int M2 = sizeof(B2) / sizeof(B2[0]); // Function Call isPossible(A2, B2, N2, M2, X2); return 0; }

Time Complexity: O(N * X)  
Auxiliary Space: O(X)

Editor: Naga



Read other contents from Beritaja.com at
More Source



close