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 = 8
Output: 5
Explanation: The subsequences having average 8 are {8}, {9, 7}, {7, 9}, {9, 7, 8}, {7, 8, 9}.
Input: arr[] = {5, 5, 1}, K = 4
Output: 0
Explanation: 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(2N)
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 ith element is included, then dp[i + 1][k + 1][s + arr[i]] += dp[i][k][s]
If ith element is excluded, then dp[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
#include <bits/stdc++.h>
using namespace std;
int dp[101][101][1001];
int countAverage( int n, int K, int * arr)
{
dp[0][0][0] = 1;
for ( int i = 0; i < n; i++) {
for ( int k = 0; k < n; k++) {
for ( int s = 0; s <= 1000; s++) {
dp[i + 1][k + 1][s + arr[i]]
+= 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][K * j];
}
return cnt;
}
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
import java.util.*;
class GFG
{
static int [][][]dp = new int [ 101 ][ 101 ][ 1001 ];
static int countAverage( int n, int K, int [] arr)
{
dp[ 0 ][ 0 ][ 0 ] = 1 ;
for ( int i = 0 ; i < n; i++)
{
for ( int k = 0 ; k < n; k++)
{
for ( int s = 0 ; s <= 100 ; s++)
{
dp[i + 1 ][k + 1 ][s + arr[i]]
+= 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][K * j];
}
return cnt;
}
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));
}
}
|
Python3
dp = [[[ 0 for i in range ( 1001 )] for i in range ( 101 )] for i in range ( 101 )]
def countAverage(n, K, arr):
global dp
dp[ 0 ][ 0 ][ 0 ] = 1
for i in range (n):
for k in range (n):
for s in range ( 100 ):
dp[i + 1 ][k + 1 ][s + arr[i]] + = dp[i][k][s]
dp[i + 1 ][k][s] + = dp[i][k][s]
cnt = 0
for j in range ( 1 , n + 1 ):
cnt + = dp[n][j][K * j]
return cnt
if __name__ = = '__main__' :
arr = [ 9 , 7 , 8 , 9 ]
K = 8
N = len (arr)
print (countAverage(N, K, arr))
|
C#
using System;
public class GFG
{
static int [,,]dp = new int [101, 101, 1001];
static int countAverage( int n, int K, int [] arr)
{
dp[0, 0, 0] = 1;
for ( int i = 0; i < n; i++)
{
for ( int k = 0; k < n; k++)
{
for ( int s = 0; s <= 100; s++)
{
dp[i + 1, k + 1, s + arr[i]]
+= 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, K * j];
}
return cnt;
}
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));
}
}
|
Javascript
<script>
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 countAverage(n, K, arr)
{
dp[0][0][0] = 1;
for ( var i = 0; i < n; i++) {
for ( var k = 0; k < n; k++) {
for ( var s = 0; s <= 1000; s++) {
dp[i + 1][k + 1][s + arr[i]]
+= dp[i][k][s];
dp[i + 1][k][s] += dp[i][k][s];
}
}
}
var cnt = 0;
for ( var j = 1; j <= n; j++) {
cnt += dp[n][j][K * j];
}
return cnt;
}
var arr = [9, 7, 8, 9];
var K = 8;
var N = arr.length
document.write( countAverage(N, K, arr));
</script>
|
Time Complexity: O(S*N2) where S is the sum of the array.
Auxiliary Space: O(S*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 array to store the computations.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int countAverage( int n, int K, int * arr)
{
int dp[101][1001] = {0};
dp[0][0] = 1;
for ( int i = 0; i < n; i++) {
for ( int k = n; k > 0; k--) {
for ( int s = 1000; s >= arr[i]; s--) {
dp[k][s] += dp[k - 1][s - arr[i]];
}
}
}
int cnt = 0;
for ( int j = 1; j <= n; j++) {
cnt += dp[j][K * j];
}
return cnt;
}
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
public class CountAverageSubsequences {
static int countAverage( int n, int K, int [] arr)
{
int [][] dp = new int [ 101 ][ 1001 ];
dp[ 0 ][ 0 ] = 1 ;
for ( int i = 0 ; i < n; i++) {
for ( int k = n; k > 0 ; k--) {
for ( int s = 1000 ; s >= arr[i]; s--) {
dp[k][s] += dp[k - 1 ][s - arr[i]];
}
}
}
int cnt = 0 ;
for ( int j = 1 ; j <= n; j++) {
cnt += dp[j][K * j];
}
return cnt;
}
public static void main(String[] args)
{
int [] arr = { 9 , 7 , 8 , 9 };
int K = 8 ;
int N = arr.length;
System.out.println(countAverage(N, K, arr));
}
}
|
Python
def count_average(n, K, arr):
dp = [[ 0 for _ in range ( 1001 )] for _ in range ( 101 )]
dp[ 0 ][ 0 ] = 1
for i in range (n):
for k in range (n, 0 , - 1 ):
for s in range ( 1000 , arr[i] - 1 , - 1 ):
dp[k][s] + = dp[k - 1 ][s - arr[i]]
cnt = 0
for j in range ( 1 , n + 1 ):
cnt + = dp[j][K * j]
return cnt
def main():
arr = [ 9 , 7 , 8 , 9 ]
K = 8
N = len (arr)
print (count_average(N, K, arr))
if __name__ = = "__main__" :
main()
|
C#
using System;
class GFG {
static int CountAverage( int n, int K, int [] arr)
{
int [, ] dp = new int [101, 1001];
dp[0, 0] = 1;
for ( int i = 0; i < n; i++) {
for ( int k = n; k > 0; k--) {
for ( int s = 1000; s >= arr[i]; s--) {
dp[k, s] += dp[k - 1, s - arr[i]];
}
}
}
int cnt = 0;
for ( int j = 1; j <= n; j++) {
cnt += dp[j, K * j];
}
return cnt;
}
static void Main( string [] args)
{
int [] arr = { 9, 7, 8, 9 };
int K = 8;
int N = arr.Length;
Console.WriteLine(CountAverage(N, K, arr));
}
}
|
Javascript
function countAverage(N, K, arr) {
const dp = new Array(101);
for (let i = 0; i <= 100; i++) {
dp[i] = new Array(1001).fill(0);
}
dp[0][0] = 1;
for (let i = 0; i < N; i++) {
for (let k = N; k > 0; k--) {
for (let s = 1000; s >= arr[i]; s--) {
dp[k][s] += dp[k - 1][s - arr[i]];
}
}
}
let cnt = 0;
for (let j = 1; j <= N; j++) {
cnt += dp[j][K * j];
}
return cnt;
}
const arr = [9, 7, 8, 9];
const K = 8;
const N = arr.length;
console.log(countAverage(N, K, arr));
|
Time Complexity: O(S*N2) where S is the sum of the array.
Auxiliary Space: O(S*N)