# Count subsequences having average of its elements equal to K

Given an array **arr[] **consisting of **N** integers and an integer **K**, the task is to count the number of subsequences of the given array with average **K**.

**Examples:**

Input:arr[] = {9, 7, 8, 9}, K = 8Output:5Explanation:The subsequences having average 8 are {8}, {9, 7}, {7, 9}, {9, 7, 8}, {7, 8, 9}.

Input:arr[] = {5, 5, 1}, K = 4Output:0Explanation:No such subsequence can be obtained

**Naive Approach:** The simplest approach to solve the problem is to use recursion. Follow the steps below to solve the problem:

- Two options are possible for each array element, i.e. either to include the current element in the sum or to exclude the current element in the sum and increase the current index for each recursive call.
- For both the above possibilities, return the number of subsequences with average
**K**. - The base case is to check if the last index has been reached or not. The average in the base case can be calculated by dividing the sum of array elements included in that subsequence.
- If the average is equal to
**K**, then return**1**. Otherwise, return**0**.

**Time Complexity:** O(2^{N})**Auxiliary Space:** O(N)

**Efficient Approach:** The above approach can be optimized using Dynamic Programming. Follow the steps below to solve the problem:

- Initialize a 3D array, say
**dp[][][]**, where**dp[i][k][s]**is the number of ways to select**k**integers from the first**i**integers such that the sum of the selected integers is**s**. - Traverse the array.
- Two possible options are available for each element, i.e. either to include it or exclude it.
- If the current element at index
**i**is included, then the next index state will be**i + 1**and the count of selected elements will increase from**k**to**k + 1**and the sum**s**will increase to**s + arr[i]**. - If the current element at index
**i**is excluded, only index**i**will increase to**i+1**and all the other states will remain the same. - Below is the dp transition state for this problem:

If i

^{th}element is included, thendp[i + 1][k + 1][s + arr[i]] += dp[i][k][s]

If i^{th}element is excluded, thendp[i + 1][k][s] += dp[i][k][s]

- Finally, the answer will be the summation of
**dp[N][j][K*j]**for all**j**(*1 â‰¤ j â‰¤ N*.

Below is the implementation of the above approach:

## C++14

`// C++ program for the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Stores the dp states` `int` `dp[101][101][1001];` `// Function to find the count of` `// subsequences having average K` `int` `countAverage(` `int` `n, ` `int` `K, ` `int` `* arr)` `{` ` ` `// 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++) {` ` ` `// Recurrence relation` ` ` `dp[i + 1][k + 1][s + arr[i]]` ` ` `+= dp[i][k][s];` ` ` `dp[i + 1][k][s] += dp[i][k][s];` ` ` `}` ` ` `}` ` ` `}` ` ` `// Stores the sum of dp[n][j][K*j]` ` ` `// all possible values of j with` ` ` `// average K and sum K * j` ` ` `int` `cnt = 0;` ` ` `// Iterate over the range [1, N]` ` ` `for` `(` `int` `j = 1; j <= n; j++) {` ` ` `cnt += dp[n][j][K * j];` ` ` `}` ` ` `// Return the final count` ` ` `return` `cnt;` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `arr[] = { 9, 7, 8, 9 };` ` ` `int` `K = 8;` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]);` ` ` `cout << countAverage(N, K, arr);` ` ` `return` `0;` `}` |

## Java

`// Java program for the above approach` `import` `java.util.*;` `class` `GFG` `{` `// Stores the dp states` `static` `int` `[][][]dp = ` `new` `int` `[` `101` `][` `101` `][` `1001` `];` `// Function to find the count of` `// subsequences having average K` `static` `int` `countAverage(` `int` `n, ` `int` `K, ` `int` `[] arr)` `{` ` ` `// 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 <= ` `100` `; s++)` ` ` `{` ` ` `// Recurrence relation` ` ` `dp[i + ` `1` `][k + ` `1` `][s + arr[i]]` ` ` `+= dp[i][k][s];` ` ` `dp[i + ` `1` `][k][s] += dp[i][k][s];` ` ` `}` ` ` `}` ` ` `}` ` ` `// Stores the sum of dp[n][j][K*j]` ` ` `// all possible values of j with` ` ` `// average K and sum K * j` ` ` `int` `cnt = ` `0` `;` ` ` `// Iterate over the range [1, N]` ` ` `for` `(` `int` `j = ` `1` `; j <= n; j++)` ` ` `{` ` ` `cnt += dp[n][j][K * j];` ` ` `}` ` ` `// Return the final count` ` ` `return` `cnt;` `}` `// Driver Code` `public` `static` `void` `main(String[] args)` `{` ` ` `int` `arr[] = { ` `9` `, ` `7` `, ` `8` `, ` `9` `};` ` ` `int` `K = ` `8` `;` ` ` `int` `N = arr.length;` ` ` `System.out.print(countAverage(N, K, arr));` `}` `}` `// This code is contributed by shikhasingrajput` |

## Python3

`# Python3 program for the above approach` `# Stores the dp states` `dp ` `=` `[[[` `0` `for` `i ` `in` `range` `(` `1001` `)] ` `for` `i ` `in` `range` `(` `101` `)] ` `for` `i ` `in` `range` `(` `101` `)]` `# Function to find the count of` `# subsequences having average K` `def` `countAverage(n, K, arr):` ` ` `global` `dp` ` ` `dp[` `0` `][` `0` `][` `0` `] ` `=` `1` ` ` `# Three loops for three states` ` ` `for` `i ` `in` `range` `(n):` ` ` `for` `k ` `in` `range` `(n):` ` ` `for` `s ` `in` `range` `(` `100` `):` ` ` `# Recurrence relation` ` ` `dp[i ` `+` `1` `][k ` `+` `1` `][s ` `+` `arr[i]] ` `+` `=` `dp[i][k][s]` ` ` `dp[i ` `+` `1` `][k][s] ` `+` `=` `dp[i][k][s]` ` ` `# Stores the sum of dp[n][j][K*j]` ` ` `# all possible values of j with` ` ` `# average K and sum K * j` ` ` `cnt ` `=` `0` ` ` `# Iterate over the range [1, N]` ` ` `for` `j ` `in` `range` `(` `1` `, n ` `+` `1` `):` ` ` `cnt ` `+` `=` `dp[n][j][K ` `*` `j]` ` ` `# Return the final count` ` ` `return` `cnt` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` `arr` `=` `[` `9` `, ` `7` `, ` `8` `, ` `9` `]` ` ` `K ` `=` `8` ` ` `N ` `=` `len` `(arr)` ` ` `print` `(countAverage(N, K, arr))` ` ` `# This code is contributed by mohit kumar 29.` |

## C#

`// C# program for the above approach` `using` `System;` `public` `class` `GFG` `{` `// Stores the dp states` `static` `int` `[,,]dp = ` `new` `int` `[101, 101, 1001];` `// Function to find the count of` `// subsequences having average K` `static` `int` `countAverage(` `int` `n, ` `int` `K, ` `int` `[] arr)` `{` ` ` `// 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 <= 100; s++)` ` ` `{` ` ` `// Recurrence relation` ` ` `dp[i + 1, k + 1, s + arr[i]]` ` ` `+= dp[i, k, s];` ` ` `dp[i + 1, k, s] += dp[i, k, s];` ` ` `}` ` ` `}` ` ` `}` ` ` `// Stores the sum of dp[n,j,K*j]` ` ` `// all possible values of j with` ` ` `// average K and sum K * j` ` ` `int` `cnt = 0;` ` ` `// Iterate over the range [1, N]` ` ` `for` `(` `int` `j = 1; j <= n; j++)` ` ` `{` ` ` `cnt += dp[n, j, K * j];` ` ` `}` ` ` `// Return the readonly count` ` ` `return` `cnt;` `}` `// Driver Code` `public` `static` `void` `Main(String[] args)` `{` ` ` `int` `[]arr = { 9, 7, 8, 9 };` ` ` `int` `K = 8;` ` ` `int` `N = arr.Length;` ` ` `Console.Write(countAverage(N, K, arr));` `}` `}` `// This code is contributed by 29AjayKumar` |

## Javascript

`<script>` `// JavaScript program for the above approach` `// Stores the dp states` `var` `dp = Array.from(Array(101), ()=> Array(101));` `for` `(` `var` `i =0; i<101; i++)` ` ` `for` `(` `var` `j =0; j<101; j++)` ` ` `dp[i][j] = ` `new` `Array(1001).fill(0);` `// Function to find the count of` `// subsequences having average K` `function` `countAverage(n, K, arr)` `{` ` ` `// Base condition` ` ` `dp[0][0][0] = 1;` ` ` `// Three loops for three states` ` ` `for` `(` `var` `i = 0; i < n; i++) {` ` ` `for` `(` `var` `k = 0; k < n; k++) {` ` ` `for` `(` `var` `s = 0; s <= 1000; s++) {` ` ` `// Recurrence relation` ` ` `dp[i + 1][k + 1][s + arr[i]]` ` ` `+= dp[i][k][s];` ` ` `dp[i + 1][k][s] += dp[i][k][s];` ` ` `}` ` ` `}` ` ` `}` ` ` `// Stores the sum of dp[n][j][K*j]` ` ` `// all possible values of j with` ` ` `// average K and sum K * j` ` ` `var` `cnt = 0;` ` ` `// Iterate over the range [1, N]` ` ` `for` `(` `var` `j = 1; j <= n; j++) {` ` ` `cnt += dp[n][j][K * j];` ` ` `}` ` ` `// Return the final count` ` ` `return` `cnt;` `}` `// Driver Code` `var` `arr = [9, 7, 8, 9];` `var` `K = 8;` `var` `N = arr.length` `document.write( countAverage(N, K, arr));` `</script>` |

**Output:**

5

**Time Complexity:** O(S*N^{2}) where S is the sum of the array.**Auxiliary Space:** O(S*N^{2})