Sum of elements of all partitions of number such that no element is less than K
Last Updated :
13 Jan, 2023
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++
// 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;
}
Java
// 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
Python3
# Python3 implementation of above approach
# Function that returns total number of valid
# partitions of integer N
def countPartitions(n, k):
# Global declaration of 2D dp array
# which will be later used for memoization
dp = [[0] * 201] * 201
# Initializing 2D dp array with -1
# we will use this 2D array for memoization
for i in range(n + 1):
for j in range(n + 1):
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
answer = 1
# For loop to iterate over K to N
# and find number of possible valid
# partitions recursively.
for i in range(k, n):
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
n = 10
k = 3
# Printing total number of valid partitions
print("Total Aggregate sum of all "
"Valid Partitions: ",
countPartitions(n, k) * n)
# This code is contributed by sanjoy_62
C#
// 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_
}
JavaScript
<script>
// Javascript implementation of above approach
// Function that returns
// total number of valid
// partitions of integer N
function countPartitions(n, k)
{
// Global declaration of 2D
// dp array which will be
// later used for memoization
let dp = new Array(201);
// initializing 2D dp array
// with -1 we will use this
// 2D array for memoization
for (let i = 0; i < n + 1; i++)
{
dp[i] = new Array(201);
for (let 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
let answer = 1;
// for loop to iterate over
// K to N and find number of
// possible valid partitions
// recursively.
for (let 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;
}
let n = 10, k = 3;
// Printing total number
// of valid partitions
document.write("Total Aggregate sum of " +
"all Valid Partitions: " +
countPartitions(n, k) * n);
// This code is contributed by rameshtravel07.
</script>
Output: Total Aggregate sum of all Valid Partitions: 50
Time Complexity: O(N2)
Auxiliary Space: O(201*201) = O(1)
Similar Reads
Partition into two subsets of lengths K and (N - k) such that the difference of sums is maximum Given an array of non-negative integers of length N and an integer K. Partition the given array into two subsets of length K and N - K so that the difference between the sum of both subsets is maximum. Examples : Input : arr[] = {8, 4, 5, 2, 10} k = 2 Output : 17 Explanation : Here, we can make firs
7 min read
Min sum of set of positive integers so that sum of no two elements is K Given two positive integers N and K. Create a set of size N containing distinct positive integers such that the sum of no two integers is K, the task is to find the minimum possible sum of the set that meets the above criteria. Since the answer may be large, print it modulo 10^9+7. Note: It is guara
6 min read
Count of permutations such that sum of K numbers from given range is even Given a range [low, high], both inclusive, and an integer K, the task is to select K numbers from the range(a number can be chosen multiple times) such that the sum of those K numbers is even. Print the number of all such permutations. Examples: Input: low = 4, high = 5, k = 3 Output: 4 Explanation:
7 min read
Partitions possible such that the minimum element divides all the other elements of the partition Given an integer array arr[], the task is to count the number of partitions possible such that in each partition the minimum element divides all the other elements of the partition. The partition need not be continuous.Examples: Input: arr[] = {10, 7, 20, 21, 13} Output: 3 The possible partitions ar
6 min read
Total cuts such that sum of largest of left and smallest of right is atleast K Given an array A[] of size N and an integer K, the task is to find the total number of cuts that you can make such that for each cut these two conditions are satisfied: A cut divides an array into two parts of equal or unequal length (non-zero).Sum of the largest element in the left part and the sma
6 min read
Count possible splits of sum N into K integers such that the minimum is at least P Given three integers N, P, and K, the task is to find the total number of ways to divide N into K integers having sum N where each integer is ? P. Examples: Input: K = 3, N = 8, P = 2Output: 6Explanation:Six possible solutions are: {2, 2, 4}, {2, 3, 3}, {2, 4, 2}, {3, 2, 3}, {3, 3, 2}, {4, 2, 2}Each
14 min read
Maximize sum of second minimums of each K length partitions of the array Given an array A[] of size N and a positive integer K ( which will always be a factor of N), the task is to find the maximum possible sum of the second smallest elements of each partition of the array by partitioning the array into (N / K) partitions of equal size. Examples: Input: A[] = {2, 3, 1, 4
6 min read
Count of all possible combinations of K numbers that sums to N Given a number N, the task is to count the combinations of K numbers from 1 to N having a sum equal to N, with duplicates allowed. Example: Input: N = 7, K = 3Output:15Explanation:The combinations which lead to the sum N = 7 are: {1, 1, 5}, {1, 5, 1}, {5, 1, 1}, {2, 1, 4}, {1, 2, 4}, {1, 4, 2}, {2,
11 min read
Minimum partitions of String such that each part is at most K Given a string S of size N consisting of numerical digits 1-9 and a positive integer K, the task is to minimize the partitions of the string such that each part is at most K. If itâs impossible to partition the string print -1. Examples: Input: S = "3456", K = 45Output: 2Explanation: One possible wa
8 min read
Number of arrays of size N whose elements are positive integers and sum is K Given two positive integers N and K. The task is to find the number of arrays of size N that can be formed such that elements of the array should be positive integers and the sum of elements is equal to K. Examples: Input : N = 2, K = 3 Output : 2 Explanation: [1, 2] and [2, 1] are the only arrays o
6 min read