Ways to write n as sum | Practice | GeeksforGeeks

Given a positive integer N, the task is to find the number of different ways in which N can be written as a sum of two or more positive integers.
Example 1:
Input:
N = 5
Output: 6
Explanation: 
1+1+1+1+1
1+1+1+2
1+1+3
1+4
2+1+2
2+3
So, a total of 6 ways.

Example 2:
Input:
N = 3
Output: 2

Your Task:
Your task is to complete the function countWays() which takes 1 argument(N) and returns the answer%(10^9 + 7).
Expected Time Complexity: O(N2)
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 103
0
imranwahid1 week ago
Easy C++ solution
0
chessnoobdj4 weeks ago
Approach : 
dp[0] = 1
dp[i] = dp[i-1] + dp[i-2] + ... + dp[0]
int countWays(int n)
    {
        int mod = 1e9+7;
        vector <int> dp(n+1, 0);
        dp[0] = 1;
        for (int i=1; i<n; i++)
            for (int j=i; j<=n; j++)
                dp[j] += dp[j-i]%mod;
        return dp[n];
    }
+1
kharsh8221 month ago
Java Recursive And Iterative Method
// { Driver Code Starts
import java.util.*;
import java.lang.*;
import java.io.*;

class GFG{

  public static void main (String[] args)  {
     Scanner s=new Scanner(System.in);
     int t=s.nextInt();
     while(t-->0){
         int n=s.nextInt();
         Solution obj = new Solution();
         System.out.println(obj.countWays(n));
     }

   }
}
// } Driver Code Ends




class Solution
{
    // function to count ways in which n can be
    // expressed as the sum of two or more integers
    int countWays(int n)
    {
    
         int [] arr =new int [n];
         
         for(int i=0;i<arr.length;i++){
             arr[i]=i;
         }
         
        //  return helper(arr,n,n-1);
        
        int [][] arr1=new int[n][n+1];
        
        for(int i =0;i<arr1.length;i++){
            for(int j =0;j<arr1[i].length;j++){
                if(i==0){
                    arr1[i][j]=0;
                }
                
                if(j==0){
                    arr1[i][j]=1;
                }
            }
        }
        
        for(int i =1;i<arr1.length;i++){
            for(int j =1;j<arr1[i].length;j++){
                
                if(arr[i-1]<j){
                    arr1[i][j]=arr1[i][j-arr[i]]+arr1[i-1][j];
                }
                else{
                    arr1[i][j]=arr1[i-1][j];
                }
            }
        }
        
        return arr1[n-1][n];
        
    }
    
    int helper(int [] arr ,int n ,int m ){
        
        if(n==0 ){
            return 1 ;
        }
        
        if(m==0){
            return 0;
        }
        
        
        if(arr[m-1]<n){
            
            return helper(arr,n-arr[m],m)+helper(arr,n,m-1);
        }
        else{
            return helper(arr,n,m-1);
        }
    }
}

0
noobcoder1533 months ago
  int coins[n-1];          for(int i=0;i<n-1;i++)          coins[i]=i+1;   //here coins array contain coin from 1 to n-1                           int dp[n][n+1];            for(int i=0;i<n+1;i++)       dp[0][i]=0;       for(int i=0;i<n;i++)       {           dp[i][0]=1;       }             for(int i=1;i<n;i++)       {           for(int j=1;j<n+1;j++)           {               if(coins[i-1]<=j)               dp[i][j]=dp[i-1][j]+dp[i][j-coins[i-1]];               else               dp[i][j]=dp[i-1][j];           }       }       return dp[n-1][n];
0
sk77887754 months ago
// Unbounded knapsack
class Solution
{
    // function to count ways in which n can be
    // expressed as the sum of two or more integers
    int countWays(int n)
    {
        // your code here
        int w=n;n=n-1;
        long[] arr=new long[n];
        for(int i=0;i<arr.length;i++)
            arr[i]=i+1;
        long[][] dp=new long[n+1][w+1];
        for(int i=0;i<=n;i++) dp[i][0]=1;
        for(int i=1;i<=n;i++) {
            for(int j=1;j<=w;j++) {
                if(arr[i-1]<=j)
                    dp[i][j]=dp[i][j-(int)arr[i-1]]+dp[i-1][j];
                else
                    dp[i][j]=dp[i-1][j];
            }
        }
        return (int)dp[n][w];
    }
}
0
abhishekguptaaa4 months ago
class Solution{   public:      int mod=1000000007;      //Function to count the number of different ways in which n can be    //written as a sum of two or more positive integers.   int countWays(int n)   {       // your code here              vector<int> v;              for(int i=1;i<n;i++)       {           v.push_back(i);       }              int sz=v.size();              int dp[sz+1][n+1];              for(int i=0;i<sz+1;i++)       {           for(int j=0;j<n+1;j++)           {               if(i==0)               {                   dp[i][j]=0;               }               else if(j==0)               {                   dp[i][j]=1;               }           }       }                     for(int i=1;i<sz+1;i++)       {           for(int j=1;j<n+1;j++)           {               if(v[i-1]<=j)               {                   dp[i][j]=(dp[i-1][j]%mod + dp[i][j-v[i-1]]%mod)%mod;               }               else               {                   dp[i][j]=dp[i-1][j]%mod;               }           }       }                     return dp[sz][n]%mod;                        }};
+4
kanishk98715 months ago
C++ DP Implementation (Time Taken: 0.0/1.1)
int countWays(int n)
    {
        // your code here
        int dp[n+1];
        memset(dp, 0, sizeof(dp));
        dp[0] = 1;
        
        for(int i = 1; i < n; i++){
            for(int j = i; j <= n; j++){
                dp[j] += dp[j - i];
            }
        }
        
        return dp[n];
    }
0
Abhineet Sarkar8 months ago
Abhineet Sarkar
Any successful python submissions?
0
Sakshi Agarwal9 months ago
Sakshi Agarwal
 int countWays(int n)    {        // your code here        long long dp[n][n+1];        memset(dp,0,sizeof(dp));        for(int i=0;i<n;i++){ dp[i][0]="1;" }="" for(int="" i="1;i&lt;n;i++){" for(int="" j="1;j&lt;=n;j++){" if(i="">j){                    dp[i][j] = dp[i-1][j];                }                else{                    dp[i][j] = (dp[i-1][j] + dp[i][j-i])%1000000007;                }            }        }        return dp[n-1][n];    }
+1
Abhishek Saxena9 months ago
Abhishek Saxena
int countWays(int n)
    {
        // your code here
        int dp[n+1]={0};
        dp[0]=1;
        long long m=1000000007;
        for(int i=1;i<=n-1;i++){
            for(int j=i;j<=n;j++){
                dp[j]=(dp[j]%m+dp[j-i]%m)%m;
            }
        }
        return dp[n]%m;
    }
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.