Sum of elements of all partitions of number such that no element is less than K

Given an integer N, the task is to find an aggregate sum of all integer partitions of this number such that each partition does not contain any integer less than K.

Examples:

Input: N = 6 and K = 2
Output: 24
In this case, there are 4 valid partitions.
1) {6}
2) {4, 2}
3) {3, 3}
4) {2, 2, 2}
Therefore, aggregate sum would be
6 + 4 + 2 + 3 + 3 + 2 + 2 + 2 = 24



Input: N = 10 and K = 3
Output: 50
Here, 5 valid partitions are:
1) {10}
2) {7, 3}
3) {6, 4}
4) {5, 5}
5) {3, 3, 4}
Aggregate sum in this case would be
10 + 7 + 3 + 6 + 4 + 5 + 5 + 3 + 3 + 4 = 50

Approach: This problem has a simple recursive solution. First, we need to count the total number of valid partitions of number N such that each partition contains integers greater than or equal to K. So we will iteratively apply our recursive solution to find valid partitions that have the minimum integer K, K+1, K+2, …, N.
Our final answer would be N * no of valid partitions because each valid partition has a sum equal to N.

Following are some key ideas for designing recursive function to find total number of valid partitions.

  • If N < K then no partition is possible.
  • If N < 2*K then only one partition is possible and that is the number N itself.
  • We can find number partitions in a recursive manner that contains integers at least equal to ‘i’ (‘i’ can be from K to N) and add them all to get final answer.

Pseudo code for recursive function to find number of valid partitions:

f(N,K):
       if N < K
           return 0
       if N < 2K
           return 1
       Initialize answer = 1
       FOR i from K to N
           answer = answer + f(N-i,i)
return answer 

Below is the Dynamic Programming solution:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
  
// Function that returns total number of valid
// partitions of integer N
long long int countPartitions(int n, int k)
{
  
      
    // Global declaration of 2D dp array
    // which will be later used for memoization
    long long int dp[201][201];
  
    // initializing 2D dp array with -1
    // we will use this 2D array for memoization
    for (int i = 0; i < n + 1; i++) {
        for (int j = 0; j < n + 1; j++) {
            dp[i][j] = -1;
        }
    }
  
    // if this subproblem is already previously
    // calculated, then directly return that answer
    if (dp[n][k] >= 0)
        return dp[n][k];
  
    // if N < K, then no valid 
    // partition is possible
    if (n < k)
        return 0;
  
    // if N is between K to 2*K then
    // there is only one
    // partition and that is the number N itself
    if (n < 2 * k)
        return 1;
  
    // Initialize answer with 1 as 
    // the number N itself
    // is always a valid partition
    long long int answer = 1;
  
    // for loop to iterate over K to N
    // and find number of
    // possible valid partitions recursively.
    for (int i = k; i < n; i++)
        answer = answer + countPartitions(n - i, i);
  
    // memoization is done by storing
    // this calculated answer
    dp[n][k] = answer;
  
    // returning number of valid partitions
    return answer;
}
  
// Driver code
int main()
{
    int n = 10, k = 3;
  
    // Printing total number of valid partitions
    cout << "Total Aggregate sum of all Valid Partitions: "
         << countPartitions(n, k) * n;
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of
// above approach
class GFG
{
// Function that returns
// total number of valid
// partitions of integer N
static long countPartitions(int n, int k)
{
  
    // Global declaration of 2D
    // dp array which will be 
    // later used for memoization
    long[][] dp = new long[201][201];
  
    // initializing 2D dp array 
    // with -1 we will use this 
    // 2D array for memoization
    for (int i = 0; i < n + 1; i++)
    {
        for (int j = 0; j < n + 1; j++)
        {
            dp[i][j] = -1;
        }
    }
  
    // if this subproblem is already 
    // previously calculated, then 
    // directly return that answer
    if (dp[n][k] >= 0)
        return dp[n][k];
  
    // if N < K, then no valid 
    // partition is possible
    if (n < k)
        return 0;
  
    // if N is between K to 2*K 
    // then there is only one
    // partition and that is 
    // the number N itself
    if (n < 2 * k)
        return 1;
  
    // Initialize answer with 1
    // as the number N itself
    // is always a valid partition
    long answer = 1;
  
    // for loop to iterate over 
    // K to N and find number of
    // possible valid partitions
    // recursively.
    for (int i = k; i < n; i++)
        answer = answer + 
                 countPartitions(n - i, i);
  
    // memoization is done by storing
    // this calculated answer
    dp[n][k] = answer;
  
    // returning number of
    // valid partitions
    return answer;
}
  
// Driver code
public static void main(String[] args)
{
    int n = 10, k = 3;
  
    // Printing total number
    // of valid partitions
    System.out.println("Total Aggregate sum of "
                       "all Valid Partitions: "
                       countPartitions(n, k) * n);
}
}
  
// This code is contributed by mits

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of above approach
using System; 
    
class GFG 
    // Function that returns total number of valid 
    // partitions of integer N 
    public static long countPartitions(int n, int k) 
    
        
        // Global declaration of 2D dp array 
        // which will be later used for memoization 
        long[,] dp = new long[201,201]; 
        
        // initializing 2D dp array with -1 
        // we will use this 2D array for memoization 
        for (int i = 0; i < n + 1; i++) { 
            for (int j = 0; j < n + 1; j++) { 
                dp[i,j] = -1; 
            
        
        
        // if this subproblem is already previously 
        // calculated, then directly return that answer 
        if (dp[n,k] >= 0) 
            return dp[n,k]; 
        
        // if N < K, then no valid  
        // partition is possible 
        if (n < k) 
            return 0; 
        
        // if N is between K to 2*K then 
        // there is only one 
        // partition and that is the number N itself 
        if (n < 2 * k) 
            return 1; 
        
        // Initialize answer with 1 as  
        // the number N itself 
        // is always a valid partition 
        long answer = 1; 
        
        // for loop to iterate over K to N 
        // and find number of 
        // possible valid partitions recursively. 
        for (int i = k; i < n; i++) 
            answer = answer + countPartitions(n - i, i); 
        
        // memoization is done by storing 
        // this calculated answer 
        dp[n,k] = answer; 
        
        // returning number of valid partitions 
        return answer; 
    
        
    // Driver code  
    static void Main() 
    
        int n = 10, k = 3; 
    
        // Printing total number of valid partitions 
        Console.Write("Total Aggregate sum of all Valid Partitions: " + countPartitions(n, k) * n); 
    }
    //This code is contributed by DrRoot_
}

chevron_right


Output:

Total Aggregate sum of all Valid Partitions: 50

Time Complexity: O(N2)



My Personal Notes arrow_drop_up

A competitive coder developer and a learner by choice who is always eager to contribute to the computer science and developer community

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Mithun Kumar, DrRoot_