Given an array A[] of length N and integer F, the task is to find the number of subsequences where the average of the sum of the square of elements (of that particular subsequence) is equal to the value F.
Examples:
Input: A[] = {1, 2, 1, 2}, F = 2
Output: 2
Explanation: Two subsets with value F = 2 are {2} and {2}. Where 2^2/1 = 2Input: A[] = {1, 1, 1, 1}, F = 1
Output: 15
Explanation: All the subsets will return the function value of 1 except the empty subset. Hence the total number of subsequences will be 2 ^ 4 – 1 = 15.
Approach: This can be solved using the following idea:
Using Dynamic programming, we can reduce the overlapping of subsets that are already computed.
Follow the steps below to solve the problem:
- Initialize a 3D array, say dp[][][], where dp[i][k][f] indicates the number of ways to select k integers from first i values such that their sum of squares or any other function according to F is stored in dp[i][k][f]
- Traverse the array, we still have 2 options for each element i.e. to include or to exclude. The transition will be as shown at the end:
- If included then we will have k + 1 elements with functional sum value equal to s+ Â sq where sq is the square value i.e. arr[i]^2.
- If it is excluded we simply traverse to the next index storing the previous state in it as it is.
- Finally, the answer will be the sum of dp[N][j][F*j] as the functional value is the squared sum average.
Transitions for DP:
- Include the ith element: dp[i + 1][k + 1][s + sq] += dp[i][k][s]
- Exclude the ith element: dp[i + 1][k][s] += dp[i][k][s]
Below is the implementation of the code:
C++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;Â
// Storing the DP states// dp[i][k][f] indicates that the number// of ways to select k integers from first// i values such that their sum of squares// or any other function according to F// is stored in dp[i][k][F]int dp[101][101][1001];Â
// Calculate the number of subsequences// with given function valueint countValue(int n, int F, vector<int> v){Â
    // Base condition    dp[0][0][0] = 1;Â
    // Three loops for three states    for (int i = 0; i < n; i++) {        for (int k = 0; k < n; k++) {            for (int s = 0; s <= 1000; s++) {                long long sq                    = (long long)v[i] * (long long)v[i];Â
                // Recurrence relationÂ
                // Include the ith element                dp[i + 1][k + 1][s + sq] += dp[i][k][s];Â
                // Exclude the ith element                dp[i + 1][k][s] += dp[i][k][s];            }        }    }Â
    int cnt = 0;Â
    // Iterate over the range [1, N]    for (int j = 1; j <= n; j++) {        cnt += dp[n][j][F * j];    }Â
    // Return the final count    return cnt;}Â
// Driver Codeint main(){Â Â Â Â vector<int> v = { 1, 2, 1, 2 };Â Â Â Â int F = 2, N = v.size();Â
    // Function call    cout << countValue(N, F, v);    return 0;} |
Java
import java.util.*;Â
public class Main {    // Storing the DP states    // dp[i][k][f] indicates that the number    // of ways to select k integers from first    // i values such that their sum of squares    // or any other function according to F    // is stored in dp[i][k][F]    static long[][][] dp = new long[101][101][1001];Â
    // Calculate the number of subsequences    // with given function value    static long countValue(int n, int F, List<Integer> v) {Â
        // Base condition        dp[0][0][0] = 1;Â
        // Three loops for three states        for (int i = 0; i < n; i++) {            for (int k = 0; k < n; k++) {                for (int s = 0; s <= 1000; s++) {                    long sq = (long) v.get(i) * (long) v.get(i);Â
                    // Recurrence relationÂ
                    // Include the ith element                    if (i + 1 <= n && k + 1 <= n && s + sq <= 1000) {                        dp[i + 1][k + 1][s + (int) sq] += dp[i][k][s];                    }Â
                    // Exclude the ith element                    if (i + 1 <= n && k <= n && s <= 1000) {                        dp[i + 1][k][s] += dp[i][k][s];                    }                }            }        }Â
        long cnt = 0;Â
        // Iterate over the range [1, N]        for (int j = 1; j <= n; j++) {            if (n <= 100 && j <= 100 && F * j <= 1000) {                cnt += dp[n][j][F * j];            }        }Â
        // Return the final count        return cnt;    }Â
    // Driver code    public static void main(String[] args) {        List<Integer> v = Arrays.asList(1, 2, 1, 2);        int F = 2, N = v.size();Â
        // Function call        System.out.println(countValue(N, F, v));    }} |
Python3
# Python program for the above approachÂ
# Storing the DP states# dp[i][k][f] indicates that the number# of ways to select k integers from first# i values such that their sum of squares# or any other function according to F# is stored in dp[i][k][F]dp = [[[0 for i in range(1005)] for j in range(101)] for k in range(101)]Â
# Calculate the number of subsequences# with given function valuedef countValue(n, F, v):Â
    # Base condition    dp[0][0][0] = 1Â
    # Three loops for three states    for i in range(n):        for k in range(n):            for s in range(1001):                sq = v[i] * v[i]Â
                # Recurrence relationÂ
                # Include the ith element                dp[i + 1][k + 1][s + sq] += dp[i][k][s]Â
                # Exclude the ith element                dp[i + 1][k][s] += dp[i][k][s]Â
    cnt = 0Â
    # Iterate over the range [1, N]    for j in range(1, n + 1):        cnt += dp[n][j][F * j]Â
    # Return the final count    return cntÂ
# Driver Codeif __name__ == '__main__':Â Â Â Â v = [1, 2, 1, 2]Â Â Â Â F, N = 2, len(v)Â
    # Function call    print(countValue(N, F, v))Â
# This code is contributed by Susobhan Akhuil |
C#
// C# program for the above approachÂ
using System;Â
public class GFG {    // Storing the DP states    // dp[i][k][f] indicates that the number    // of ways to select k integers from first    // i values such that their sum of squares    // or any other function according to F    // is stored in dp[i][k][F]    static int[,,] dp = new int[101,101,1005];Â
    // Calculate the number of subsequences    // with given function value    static int countValue(int n, int F, int[] v) {Â
        // Base condition        dp[0,0,0] = 1;Â
        // Three loops for three states        for (int i = 0; i < n; i++) {            for (int k = 0; k < n; k++) {                for (int s = 0; s <= 1000; s++) {                    long sq = (long)v[i] * (long)v[i];Â
                    // Recurrence relationÂ
                    // Include the ith element                    dp[i + 1,k + 1,s + sq] += dp[i,k,s];Â
                    // Exclude the ith element                    dp[i + 1,k,s] += dp[i,k,s];                }            }        }Â
        int cnt = 0;Â
        // Iterate over the range [1, N]        for (int j = 1; j <= n; j++) {            cnt += dp[n,j,F * j];        }Â
        // Return the final count        return cnt;    }Â
    // Driver Code    public static void Main() {        int[] v = { 1, 2, 1, 2 };        int F = 2, N = v.Length;Â
        // Function call        Console.WriteLine(countValue(N, F, v));    }} |
Javascript
// Storing the DP states// dp[i][k][f] indicates that the number// of ways to select k integers from first// i values such that their sum of squares// or any other function according to F// is stored in dp[i][k][F]const dp = Array.from(Array(101), () => Array.from(Array(101), () => new Array(1005).fill(0)));Â
// Calculate the number of subsequences// with given function valuefunction countValue(n, F, v) {Â
    // Base condition    dp[0][0][0] = 1;Â
    // Three loops for three states    for (let i = 0; i < n; i++) {        for (let k = 0; k < n; k++) {            for (let s = 0; s <= 1000; s++) {                const sq = v[i] * v[i];Â
                // Recurrence relationÂ
                // Include the ith element                dp[i + 1][k + 1][s + sq] += dp[i][k][s];Â
                // Exclude the ith element                dp[i + 1][k][s] += dp[i][k][s];            }        }    }Â
    let cnt = 0;Â
    // Iterate over the range [1, N]    for (let j = 1; j <= n; j++) {        cnt += dp[n][j][F * j];    }Â
    // Return the final count    return cnt;}Â
// Driver Codeconst v = [1, 2, 1, 2];const F = 2;const N = v.length;Â
// Function callconsole.log(countValue(N, F, v)); |
2
Time Complexity:Â O(F*N2)
Auxiliary Space:Â O(F*N2)
Efficient approach : Space optimization
In previous approach the current value dp[i][j][s] is only depend upon the current and previous row values of DP. So to optimize the space complexity we use a single 2D vector to store the computations.
Implementation:Â
C++
#include <bits/stdc++.h>using namespace std;Â
// Calculate the number of subsequences// with given function valueint countValue(int n, int F, vector<int> v){    // Initialize the DP table    vector<vector<int> > dp(n + 1, vector<int>(1001, 0));    dp[0][0] = 1;Â
    // Compute the DP table    for (int i = 0; i < n; i++) {        for (int k = i + 1; k >= 1; k--) {            for (int s = 0; s <= 1000; s++) {                int sq = v[i] * v[i];Â
                // Recurrence relation                if (s + sq <= 1000) {                    dp[k][s + sq] += dp[k - 1][s];                }            }        }    }Â
    // Compute the final count    int cnt = 0;    for (int k = 1; k <= n; k++) {        cnt += dp[k][k * F];    }    return cnt;}Â
// Driver Codeint main(){Â Â Â Â vector<int> v = { 1, 2, 1, 2 };Â Â Â Â int F = 2, N = v.size();Â
    // Function call    cout << countValue(N, F, v);    return 0;} |
Java
import java.util.*;Â
public class GFG {    // Calculate the number of subsequences    // with the given function value    public static int countValue(int n, int F, ArrayList<Integer> v) {        // Initialize the DP table        int[][] dp = new int[n + 1][1001];        dp[0][0] = 1;Â
        // Compute the DP table        for (int i = 0; i < n; i++) {            for (int k = i + 1; k >= 1; k--) {                for (int s = 0; s <= 1000; s++) {                    int sq = v.get(i) * v.get(i);Â
                    // Recurrence relation                    if (s + sq <= 1000) {                        dp[k][s + sq] += dp[k - 1][s];                    }                }            }        }Â
        // Compute the final count        int cnt = 0;        for (int k = 1; k <= n; k++) {            cnt += dp[k][k * F];        }        return cnt;    }Â
    // Driver Code    public static void main(String[] args) {        ArrayList<Integer> v = new ArrayList<>(Arrays.asList(1, 2, 1, 2));        int F = 2;        int N = v.size();Â
        // Function call        System.out.println(countValue(N, F, v));    }} |
Python
# Calculate the number of subsequences# with given function valueÂ
Â
def countValue(n, F, v):Â
    # Initialize the DP table    dp = [[0] * 1001 for _ in range(n + 1)]    dp[0][0] = 1Â
    # Compute the DP table    for i in range(n):        for k in range(i + 1, 0, -1):            for s in range(1001):                sq = v[i] * v[i]Â
                # Recurrence relation                if s + sq <= 1000:                    dp[k][s + sq] += dp[k - 1][s]Â
    # Compute the final count    cnt = 0    for k in range(1, n + 1):        cnt += dp[k][k * F]Â
    return cntÂ
# Driver CodeÂ
Â
def main():Â Â Â Â v = [1, 2, 1, 2]Â Â Â Â F = 2Â Â Â Â N = len(v)Â
    # Function call    result = countValue(N, F, v)    print(result)Â
Â
if __name__ == "__main__":Â Â Â Â main() |
C#
using System;using System.Collections.Generic;Â
public class GFG{    // Calculate the number of subsequences with a given   // function value    public static int CountValue(int n, int F, List<int> v)    {        // Initialize the DP table        int[,] dp = new int[n + 1, 1001];        dp[0, 0] = 1;Â
        // Compute the DP table        for (int i = 0; i < n; i++)        {            for (int k = i + 1; k >= 1; k--)            {                for (int s = 0; s <= 1000; s++)                {                    int sq = v[i] * v[i];Â
                    // Recurrence relation                    if (s + sq <= 1000)                    {                        dp[k, s + sq] += dp[k - 1, s];                    }                }            }        }Â
        // Compute the final count        int cnt = 0;        for (int k = 1; k <= n; k++)        {            cnt += dp[k, k * F];        }        return cnt;    }Â
    public static void Main(string[] args)    {        List<int> v = new List<int> { 1, 2, 1, 2 };        int F = 2;        int N = v.Count;Â
        // Function call        int result = CountValue(N, F, v);        Console.WriteLine(result);    }} |
Javascript
function countValue(n, F, v) {    const dp = new Array(n + 1).fill(null).map(() => new Array(1001).fill(0));    dp[0][0] = 1;    // Compute the DP table    for (let i = 0; i < n; i++) {        for (let k = i + 1; k >= 1; k--) {            for (let s = 0; s <= 1000; s++) {                const sq = v[i] * v[i];                // Recurrence relation                if (s + sq <= 1000) {                    dp[k][s + sq] += dp[k - 1][s];                }            }        }    }    // Compute the final count    let cnt = 0;    for (let k = 1; k <= n; k++) {        cnt += dp[k][k * F];    }    return cnt;}// Driver Code    const v = [1, 2, 1, 2];    const F = 2;    const N = v.length;    // Function call    console.log(countValue(N, F, v)); |
2
Time Complexity:Â O(F*N^2)
Auxiliary Space:Â O(N^2)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
