Ways to sum to N using Natural Numbers up to K with repetitions allowed
Given two integers N and K, the task is to find the total number of ways of representing N as the sum of positive integers in the range [1, K], where each integer can be chosen multiple times.
Examples:
Input: N = 8, K = 2
Output: 5
Explanation: All possible ways of representing N as sum of positive integers less than or equal to K are:
- {1, 1, 1, 1, 1, 1, 1, 1}, the sum is 8.
- {2, 1, 1, 1, 1, 1, 1}, the sum is 8.
- {2, 2, 1, 1, 1, 1}, the sum is 8.
- 2, 2, 2, 1, 1}, the sum is 8.
- {2, 2, 2, 2}}, the sum is 8.
Therefore, the total number of ways is 5.
Input: N = 2, K = 2
Output: 2
Naive Approach: The simplest approach to solve the given problem is to generate all possible combinations of choosing integers over the range [1, K] and count those combinations whose sum is N.
Implementaion :
C++
#include <bits/stdc++.h>
using namespace std;
int NumberOfways( int N, int K) {
if (N == 0) return 1;
if (N < 0 || K <= 0) return 0;
return NumberOfways(N - K, K) + NumberOfways(N, K - 1);
}
int main() {
int N = 8;
int K = 2;
cout << NumberOfways(N, K) << endl;
return 0;
}
|
Java
import java.util.*;
class Main {
public static int NumberOfways( int N, int K)
{
if (N == 0 )
return 1 ;
if (N < 0 || K <= 0 )
return 0 ;
return NumberOfways(N - K, K)
+ NumberOfways(N, K - 1 );
}
public static void main(String[] args)
{
int N = 8 ;
int K = 2 ;
System.out.println(NumberOfways(N, K));
}
}
|
Python3
def number_of_ways(N, K):
if N = = 0 :
return 1
if N < 0 or K < = 0 :
return 0
return number_of_ways(N - K, K) + number_of_ways(N, K - 1 )
if __name__ = = '__main__' :
N = 8
K = 2
print (number_of_ways(N, K))
|
Javascript
function numberOfWays(N, K) {
if (N == 0) return 1;
if (N < 0 || K <= 0) return 0;
return numberOfWays(N - K, K) + numberOfWays(N, K - 1);
}
let N = 8;
let K = 2;
console.log(numberOfWays(N, K));
|
C#
using System;
class MainClass {
static int NumberOfWays( int N, int K)
{
if (N == 0)
return 1;
if (N < 0 || K <= 0)
return 0;
return NumberOfWays(N - K, K)
+ NumberOfWays(N, K - 1);
}
static void Main()
{
int N = 8;
int K = 2;
Console.WriteLine(NumberOfWays(N, K));
}
}
|
Time Complexity: O(KN)
Auxiliary Space: O(1)
Efficient Approach: The above approach has Overlapping Subproblems and an Optimal Substructure. Hence, in order to optimize, Dynamic Programming is needed to be performed based on the following observations:
- Considering dp[i] stores the total number of ways for representing i as the sum of integers lying in the range [1, K], then the transition of states can be defined as:
- For i in the range [1, K] and for every j in the range [1, N]
- The value of dp[j] is equal to (dp[j]+ dp[j – i]), for all j ? i.
Follow the steps below to solve the problem:
- Initialize an array, say dp[], with all elements as 0, to store all the recursive states.
- Initialize dp[0] as 1.
- Now, iterate over the range [1, K] using a variable i and perform the following steps:
- Iterate over the range [1, N], using a variable j, and update the value of dp[j] as dp[j]+ dp[j – i], if j ? i.
- After completing the above steps, print the value of dp[N] as the result.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int NumberOfways( int N, int K)
{
vector< int > dp(N + 1, 0);
dp[0] = 1;
for ( int row = 1; row < K + 1; row++)
{
for ( int col = 1; col < N + 1; col++)
{
if (col >= row)
dp[col] = dp[col] + dp[col - row];
}
}
return (dp[N]);
}
int main()
{
int N = 8;
int K = 2;
cout << (NumberOfways(N, K));
}
|
Java
import java.util.*;
class GFG{
static int NumberOfways( int N, int K)
{
int [] dp = new int [N + 1 ];
dp[ 0 ] = 1 ;
for ( int row = 1 ; row < K + 1 ; row++)
{
for ( int col = 1 ; col < N + 1 ; col++)
{
if (col >= row)
dp[col] = dp[col] + dp[col - row];
}
}
return (dp[N]);
}
public static void main(String[] args)
{
int N = 8 ;
int K = 2 ;
System.out.println(NumberOfways(N, K));
}
}
|
Python
def NumberOfways(N, K):
dp = [ 0 ] * (N + 1 )
dp[ 0 ] = 1
for row in range ( 1 , K + 1 ):
for col in range ( 1 , N + 1 ):
if (col > = row):
dp[col] = dp[col] + dp[col - row]
return (dp[N])
N = 8
K = 2
print (NumberOfways(N, K))
|
Javascript
<script>
function NumberOfways(N, K)
{
let dp = Array.from({length: N +1}, (_, i) => 0);
dp[0] = 1;
for (let row = 1; row < K + 1; row++)
{
for (let col = 1; col < N + 1; col++)
{
if (col >= row)
dp[col] = dp[col] + dp[col - row];
}
}
return (dp[N]);
}
let N = 8;
let K = 2;
document.write(NumberOfways(N, K));
</script>
|
C#
using System;
class GFG
{
static int NumberOfways( int N, int K)
{
int [] dp = new int [(N + 1)];
dp[0] = 1;
for ( int row = 1; row < K + 1; row++) {
for ( int col = 1; col < N + 1; col++) {
if (col >= row)
dp[col] = dp[col] + dp[col - row];
}
}
return (dp[N]);
}
public static void Main()
{
int N = 8;
int K = 2;
Console.WriteLine(NumberOfways(N, K));
}
}
|
Time Complexity: O(N * K)
Auxiliary Space: O(N)
Last Updated :
13 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...