Perfect Sum Problem | Practice | GeeksforGeeks

Given an array arr[] of non-negative integers and an integer sum, the task is to count all subsets of the given array with a sum equal to a given sum.
Note: Answer can be very large, so, output answer modulo 109+7
Example 1:
Input: N = 6, arr[] = {2, 3, 5, 6, 8, 10}
       sum = 10
Output: 3
Explanation: {2, 3, 5}, {2, 8}, {10}
Input: N = 5, arr[] = {1, 2, 3, 4, 5}
       sum = 10
Output: 3
Explanation: {1, 2, 3, 4}, {1, 4, 5}, 
             {2, 3, 5}
0
shiva109013 hours ago
int mod = (int)(1e9 + 7);
int count(int arr[],int n,int sum,vector<vector<int>>&dp){
	    if(sum == 0 && n == 0)return 1;
	    if(sum < 0 || n <= 0)return 0;
	    
	    if(dp[n][sum] != -1)return dp[n][sum]%mod;
	    
	    return dp[n][sum] = (count(arr,n - 1,sum - arr[n - 1],dp)%mod
	                            + count(arr,n- 1,sum,dp)%mod)%mod;
}
int perfectSum(int arr[], int n, int sum)
{
        // Your code goes here
        vector<vector<int>> dp(n+1,vector<int>(sum+1,-1));
        
        return count(arr,n,sum,dp)%mod;
}
0
akkeshri1404200114 hours ago
int perfectSum(int arr[], int n, int sum)
{
       // Your code goes here
       int mod=1e9+7;
       vector<vector<int>>dp(n+1,vector<int>(sum+1));
       for(int i=0;i<n+1;i++){
           for(int j=0;j<sum+1;j++){
               if(i==0){
                   dp[i][j]=0;
               }
               else if(j==0){
                   dp[i][j]=1;
               }
           }
       }
       dp[0][0]=1;
       for(int i=1;i<n+1;i++){
           for(int j=0;j<sum+1;j++){
               if(j>=arr[i-1]){
                   dp[i][j]=(dp[i-1][j]+dp[i-1][j-arr[i-1]])%mod;
               }
               else{
                   dp[i][j]=dp[i-1][j]%mod;
               }
           }
       }
       return dp[n][sum];
       
}
+1
shrishmishra1 day ago
Someone Please figure out error in my code.
public:
int mod=1000000007;int perfectSum(int arr[], int n, int sum){       int t[n+1][sum+1];       for(int i=0;i<=n;i++)       {           t[i][0]=1;       }       for(int j=1;j<=sum;j++)       {           t[0][j]=0;       }           for(int i=1;i<=n;i++)       {           for(int j=1;i<=sum;j++)       {           if(arr[i-1]>j)           {               t[i][j]=t[i-1][j]%mod;           }           else{               t[i][j]=(t[i-1][j]%mod+t[i-1][j-arr[i-1]]%mod)%mod;           }       }              }     return (t[n][sum]);  }
+1
cyrusthapa2 days ago
Both recursive + bottom up
public:
	int mod = (int)(1e9 + 7);
	int count(int arr[],int n,int sum,vector<vector<int>>&dp){
	    if(sum == 0 && n == 0)return 1;
	    if(sum < 0 || n <= 0)return 0;
	    
	    if(dp[n][sum] != -1)return dp[n][sum]%mod;
	    
	    return dp[n][sum] = (count(arr,n - 1,sum - arr[n - 1],dp)%mod
	                            + count(arr,n- 1,sum,dp)%mod)%mod;
	}
	int perfectSum(int arr[], int n, int sum){
	    
	    vector<vector<int>>dp(n + 1, vector<int>(sum + 1,0));
	   // return count(arr,n,sum,dp)%mod;
	   
	   for(int i = 0 ;i <= n;i++)
	   dp[i][ 0 ] = 1;
	   
	   for(int i = 1 ;i <= n;i++){
	       for(int summ = 0 ;summ <= sum ;summ++){
	           dp[i][summ] = dp[i - 1][summ]%mod;
	           if( summ >= arr[i - 1] )
	           dp[i][summ] = (dp[i][summ]%mod + dp[i - 1][summ - arr[i - 1]]%mod)%mod;
	       }
	   }
	   
	   
	   return dp[n][sum]%mod;
	}
0
ramanbansal98762 days ago
Please find out the mistake in my code.int perfectSum(int arr[], int n, int sum){       int dp[sum+1][n+1];       for(int i=0;i<=sum;i++){           for(int j=0;j<=n;j++){               if(i==0 && j==0){                   dp[i][j] = 1;               }                              else if(i==0 && j!=0){                   dp[i][j] = 1;               }                              else if(i!=0 && j==0){                   dp[i][j] = 0;               }               else{                   if(i>=arr[j-1]){                       dp[i][j] = dp[i-arr[j-1]][j-1] + dp[i][j-1];                   }                   else{                       dp[i][j] = dp[i][j-1];                   }               }           }       }       return dp[sum][n];}
+1
soumya1107993 days ago
dp = [[-1 for i in range(1001)] for j in range(1001)]
def subset_sum_K_count(arr, target, n):   mod= 1000000009   if n== 0:              if target == 0:           return 1              else:           return 0      if dp[n][target] != -1:       return dp[n][target]      if arr[n-1] <= target:              return subset_sum_K_count(arr, target - arr[n-1], n-1)%mod + subset_sum_K_count(arr, target, n-1)%mod                 else:              return subset_sum_K_count(arr, target, n-1)%mod
0
mitashiv01013 days ago
Memoization and Tabular method both 
 
 int m=1000000007;//memoization methodint perfect(int arr[],int n,int sum,vector<vector<int>>&dp){   // if(sum==0)return 1;    if(n==0){       // if(sum==arr[0])return 1;        if(sum==0&&arr[0]==0)return 2;        if(sum==0||arr[0]==sum)return 1;        return 0;    }          if(dp[n][sum]!=-1)return dp[n][sum];        if(sum>=arr[n])return dp[n][sum] = (perfect(arr,n-1,sum-arr[n],dp)%m+perfect(arr,n-1,sum,dp)%m)%m;    else return dp[n][sum] = perfect(arr,n-1,sum,dp)%m;    }int perfectSum(int arr[], int n, int sum){       // Your code goes here       vector<vector<int>>dp(n+1,vector<int>(sum+1,-1));              // table method       for(int i=0;i<=n;i++){           for(int j=0;j<=sum;j++){               if(i==0 && j==0)dp[i][j]=1;               else if(i==0 && j!=0) dp[i][j]=0;               else if(j>=arr[i-1])dp[i][j] = ((dp[i-1][j-arr[i-1]])%m+dp[i-1][j]%m)%m;               else dp[i][j] = dp[i-1][j]%m;                          }       }              return dp[n][sum];                     // to call memoization/recursion function              // memset(dp,-1,sizeof(dp));       // return perfect(arr,n-1,sum,dp)%m;       }
+1
chessnoobdj4 days ago
C++ dp space optimization
int perfectSum(int arr[], int n, int sum)
	{
	    int dp[sum+1] = {0}, mod = 1e9+7;
	    dp[0] = 1;
	    for(int i=0; i<n; i++){
	        for(int j=sum; j>=0; j--){
	            if(j-arr[i] >= 0)
	                dp[j] = (dp[j]+dp[j-arr[i]])%mod;
	        }
	    }
	    return dp[sum];
	}
0
aditya_njr4 days ago
// memo
int m = 1000000007;   int solve(int arr[], int sum, int ind, vector<vector<int>>&t) {    if(ind == 0){           if(sum==0 && arr[0]==0) return 2;           if(sum==0 || sum == arr[0]) return 1;           return 0;       }    if (t[ind][sum] != -1) {     return t[ind][sum] % m;    }    int notTake = solve(arr, sum, ind - 1, t) % m;    int take = 0;    if (arr[ind] <= sum) {     take = solve(arr, sum - arr[ind], ind - 1, t) % m;    }    return t[ind][sum] = (take + notTake) % m;   }   int perfectSum(int arr[], int n, int sum) {    vector<vector<int>>t(n, vector<int>(sum + 1, -1));    return solve(arr, sum, n-1, t);   }
+1
dajulal5 days ago
//dp bottom up
int perfectSum(int arr[], int n, int sum){       // Your code goes here       int mod=1000000007;       int t[n+1][sum+1];       t[0][0] = 1;       for (int i = 1; i <= sum; i++)           t[0][i] = 0;       for(int i=1;i<=n;i++){           for(int j=0;j<=sum;j++){               if(arr[i-1]<=j){                   t[i][j] = (t[i-1][j]%mod + t[i-1][j-arr[i-1]]%mod)%mod;               }               else{                   t[i][j] = t[i-1][j]%mod;               }           }       }       return t[n][sum];}  
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.