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 - 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++

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

**Editor:**Naga

**Beritaja.com**at