Answers for "count number od subset having sum eqaul to sum"

C++
0

count number od subset having sum eqaul to sum

#include <iostream>

using namespace std;
int solve(int arr[],int n,int sum)
{
    int dp[n+1][sum+1];
    for(int i=0;i<=n;i++)
    {
        dp[i][0]=1;
    }
    for(int j=1;j<=sum;j++)
    {
        dp[0][j]=0;
    }
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=sum;j++)
        {
            if(arr[i-1]<=j)
            {
                dp[i][j]=dp[i-1][j]+dp[i-1][j-arr[i-1]];
            }
            else
                dp[i][j]=dp[i-1][j];
        }
    }
    return dp[n][sum];
}
int main()
{
    int n;
    cin>>n;
    int arr[n];
    for(int i=0;i<n;i++)
    {
        cin>>arr[i];
    }
    int sum;
    cin>>sum;
    cout<<solve(arr,n,sum);
    return 0;
}
Posted by: Guest on October-04-2021

Browse Popular Code Answers by Language