HomeSoftware DevelopmentVariety of subsequences of an array with given operate worth

# Variety of subsequences of an array with given operate worth

Given an array A[] of size N and integer F, the duty is to search out the quantity of subsequences the place the common of the sum of the sq. of components (of that individual subsequence) is the same as the worth F.

Examples:

Enter: A[] = {1, 2, 1, 2}, F = 2
Output: 2
Rationalization: Two subsets with worth F = 2 are {2} and {2}. The place 2^2/1 = 2

Enter: A[] = {1, 1, 1, 1}, F = 1
Output: 15
Rationalization: All of the subsets will return the operate worth of 1 besides the empty subset. Therefore the overall variety of subsequences shall be 2 ^ 4 â€“ 1 = 15.

Strategy: This may be solved utilizing the next concept:

Utilizing Dynamic programming, we are able to cut back the overlapping of subsets which might be already computed.

Observe the steps beneath to unravel the issue:

• Initialize a 3D array, say dp[][][], the place dp[i][k][f] signifies the variety of methods to pick out okay integers from first i values such that their sum of squares or every other operate in line with F is saved in dp[i][k][f]
• Traverse the array, we nonetheless have 2 choices for every aspect i.e. to embrace or to exclude. The transition shall be as proven on the finish:
• If included then we may have okay + 1 components with useful sum worth equal to s+ Â sq the place sq is the sq. worth i.e. arr[i]^2.
• Whether it is excluded we merely traverse to the following index storing the earlier state in it as it’s.
• Lastly, the reply would be the sum of dp[N][j][F*j] because the useful worth is the squared sum common.

Transitions for DP:

• Embody the ith aspect: dp[i + 1][k + 1][s + sq] += dp[i][k][s]
• Exclude the ith aspect: dp[i + 1][k][s] += dp[i][k][s]

Beneath is the implementation of the code:

## C++

 `#embrace ` `utilizing` `namespace` `std;` `Â `Â  `int` `dp[101][101][1001];` `Â `Â  `int` `countValue(``int` `n, ``int` `F, vector<``int``> v)` `{` `Â `Â  `Â Â Â Â ` `Â Â Â Â ``dp[0][0][0] = 1;` `Â `Â  `Â Â Â Â ` `Â Â Â Â ``for` `(``int` `i = 0; i < n; i++) {` `Â Â Â Â Â Â Â Â ``for` `(``int` `okay = 0; okay < n; okay++) {` `Â Â Â Â Â Â Â Â Â Â Â Â ``for` `(``int` `s = 0; s <= 1000; s++) {` `Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ``lengthy` `lengthy` `sq` `Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ``= (``lengthy` `lengthy``)v[i] * (``lengthy` `lengthy``)v[i];` `Â `Â  `Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ` `Â `Â  `Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ` `Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ``dp[i + 1][k + 1][s + sq] += dp[i][k][s];` `Â `Â  `Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ` `Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â ``dp[i + 1][k][s] += dp[i][k][s];` `Â Â Â Â Â Â Â Â Â Â Â Â ``}` `Â Â Â Â Â Â Â Â ``}` `Â Â Â Â ``}` `Â `Â  `Â Â Â Â ``int` `cnt = 0;` `Â `Â  `Â Â Â Â ` `Â Â Â Â ``for` `(``int` `j = 1; j <= n; j++) {` `Â Â Â Â Â Â Â Â ``cnt += dp[n][j][F * j];` `Â Â Â Â ``}` `Â `Â  `Â Â Â Â ` `Â Â Â Â ``return` `cnt;` `}` `Â `Â  `int` `foremost()` `{` `Â Â Â Â ``vector<``int``> v = { 1, 2, 1, 2 };` `Â Â Â Â ``int` `F = 2, N = v.dimension();` `Â `Â  `Â Â Â Â ` `Â Â Â Â ``cout << countValue(N, F, v);` `Â Â Â Â ``return` `0;` `}`

Time Complexity:Â O(F*N2)
Auxilairy House:Â O(F*N2)

RELATED ARTICLES