Ways to sum to N using Natural Numbers up to K with repetitions allowed
Last Updated :
13 Apr, 2023
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++
//C++ program for above approach
#include <bits/stdc++.h>
using namespace std;
// Function that return Ways to sum to N using
// Natural Numbers up to K with repetitions allowed
int NumberOfways(int N, int K) {
// Base case
if (N == 0) return 1;
if (N < 0 || K <= 0) return 0;
// including and not including K in sum
return NumberOfways(N - K, K) + NumberOfways(N, K - 1);
}
// Driver code
int main() {
int N = 8;
int K = 2;
// function call
cout << NumberOfways(N, K) << endl;
return 0;
}
// this code is contributed by bhardwajji
Java
import java.util.*;
class Main {
// Function that return Ways to sum to N using
// Natural Numbers up to K with repetitions allowed
public static int NumberOfways(int N, int K)
{
// Base case
if (N == 0)
return 1;
if (N < 0 || K <= 0)
return 0;
// including and not including K in sum
return NumberOfways(N - K, K)
+ NumberOfways(N, K - 1);
}
// Driver code
public static void main(String[] args)
{
int N = 8;
int K = 2;
// function call
System.out.println(NumberOfways(N, K));
}
}
Python3
# Function that returns the number of ways to sum
# to N using natural numbers up to K with repetitions allowed
def number_of_ways(N, K):
# Base case
if N == 0:
return 1
if N < 0 or K <= 0:
return 0
# Including and not including K in sum
return number_of_ways(N - K, K) + number_of_ways(N, K - 1)
# Driver code
if __name__ == '__main__':
N = 8
K = 2
# Function call
print(number_of_ways(N, K))
JavaScript
// Function that return Ways to sum to N using
// Natural Numbers up to K with repetitions allowed
function numberOfWays(N, K) {
// Base case
if (N == 0) return 1;
if (N < 0 || K <= 0) return 0;
// including and not including K in sum
return numberOfWays(N - K, K) + numberOfWays(N, K - 1);
}
// Driver code
let N = 8;
let K = 2;
// function call
console.log(numberOfWays(N, K));
C#
using System;
class MainClass {
// Function that return Ways to sum to N using
// Natural Numbers up to K with repetitions allowed
static int NumberOfWays(int N, int K)
{
// Base case
if (N == 0)
return 1;
if (N < 0 || K <= 0)
return 0;
// including and not including K in sum
return NumberOfWays(N - K, K)
+ NumberOfWays(N, K - 1);
}
// Driver code
static void Main()
{
int N = 8;
int K = 2;
// function call
Console.WriteLine(NumberOfWays(N, K));
}
}
// This code is contributed by user_dtewbxkn77n
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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find the total number of
// ways to represent N as the sum of
// integers over the range [1, K]
int NumberOfways(int N, int K)
{
// Initialize a list
vector<int> dp(N + 1, 0);
// Update dp[0] to 1
dp[0] = 1;
// Iterate over the range [1, K + 1]
for (int row = 1; row < K + 1; row++)
{
// Iterate over the range [1, N + 1]
for (int col = 1; col < N + 1; col++)
{
// If col is greater
// than or equal to row
if (col >= row)
// Update current
// dp[col] state
dp[col] = dp[col] + dp[col - row];
}
}
// Return the total number of ways
return(dp[N]);
}
// Driver Code
int main()
{
int N = 8;
int K = 2;
cout << (NumberOfways(N, K));
}
// This code is contributed by mohit kumar 29.
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to find the total number of
// ways to represent N as the sum of
// integers over the range [1, K]
static int NumberOfways(int N, int K)
{
// Initialize a list
int[] dp = new int[N + 1];
// Update dp[0] to 1
dp[0] = 1;
// Iterate over the range [1, K + 1]
for(int row = 1; row < K + 1; row++)
{
// Iterate over the range [1, N + 1]
for(int col = 1; col < N + 1; col++)
{
// If col is greater
// than or equal to row
if (col >= row)
// Update current
// dp[col] state
dp[col] = dp[col] + dp[col - row];
}
}
// Return the total number of ways
return(dp[N]);
}
// Driver code
public static void main(String[] args)
{
// Given inputs
int N = 8;
int K = 2;
System.out.println(NumberOfways(N, K));
}
}
// This code is contributed by offbeat
Python
# Python program for the above approach
# Function to find the total number of
# ways to represent N as the sum of
# integers over the range [1, K]
def NumberOfways(N, K):
# Initialize a list
dp = [0] * (N + 1)
# Update dp[0] to 1
dp[0] = 1
# Iterate over the range [1, K + 1]
for row in range(1, K + 1):
# Iterate over the range [1, N + 1]
for col in range(1, N + 1):
# If col is greater
# than or equal to row
if (col >= row):
# Update current
# dp[col] state
dp[col] = dp[col] + dp[col - row]
# Return the total number of ways
return(dp[N])
# Driver Code
N = 8
K = 2
print(NumberOfways(N, K))
JavaScript
<script>
// Javascript implementation for the above approach
// Function to find the total number of
// ways to represent N as the sum of
// integers over the range [1, K]
function NumberOfways(N, K)
{
// Initialize a list
let dp = Array.from({length: N +1}, (_, i) => 0);
// Update dp[0] to 1
dp[0] = 1;
// Iterate over the range [1, K + 1]
for(let row = 1; row < K + 1; row++)
{
// Iterate over the range [1, N + 1]
for(let col = 1; col < N + 1; col++)
{
// If col is greater
// than or equal to row
if (col >= row)
// Update current
// dp[col] state
dp[col] = dp[col] + dp[col - row];
}
}
// Return the total number of ways
return(dp[N]);
}
// Driver Code
// Given inputs
let N = 8;
let K = 2;
document.write(NumberOfways(N, K));
</script>
C#
// C# program for the above approach
using System;
class GFG
{
// Function to find the total number of
// ways to represent N as the sum of
// integers over the range [1, K]
static int NumberOfways(int N, int K)
{
// Initialize a list
int[] dp = new int[(N + 1)];
// Update dp[0] to 1
dp[0] = 1;
// Iterate over the range [1, K + 1]
for (int row = 1; row < K + 1; row++) {
// Iterate over the range [1, N + 1]
for (int col = 1; col < N + 1; col++) {
// If col is greater
// than or equal to row
if (col >= row)
// Update current
// dp[col] state
dp[col] = dp[col] + dp[col - row];
}
}
// Return the total number of ways
return (dp[N]);
}
// Driver Code
public static void Main()
{
int N = 8;
int K = 2;
Console.WriteLine(NumberOfways(N, K));
}
}
// This code is contributed by ukasp.
Time Complexity: O(N * K)
Auxiliary Space: O(N)
Similar Reads
Ways to sum to N using array elements with repetition allowed Given a set of m distinct positive integers and a value 'N'. The problem is to count the total number of ways we can form 'N' by doing sum of the array elements. Repetitions and different arrangements are allowed. Examples : Input: arr = {1, 5, 6}, N = 7Output: 6Explanation: The different ways are:1
6 min read
Different ways to sum n using numbers greater than or equal to m Given two natural numbers n and m. The task is to find the number of ways in which the numbers that are greater than or equal to m can be added to get the sum n.Examples: Input: n = 3, m = 1Output: 3Explanation: Three different ways to get sum n such that each term is greater than or equal to m are
15 min read
Count of ways to write N as a sum of three numbers Given a positive integer N, count number of ways to write N as a sum of three numbers. For numbers which are not expressible print -1.Examples: Input: N = 4 Output: 3 Explanation: ( 1 + 1 + 2 ) = 4 ( 1 + 2 + 1 ) = 4 ( 2 + 1 + 1 ) = 4. So in total, there are 3 ways.Input: N = 5 Output: 6 ( 1 + 1 + 3
4 min read
Count of ways to write N as a sum of three numbers Given a positive integer N, count number of ways to write N as a sum of three numbers. For numbers which are not expressible print -1.Examples: Input: N = 4 Output: 3 Explanation: ( 1 + 1 + 2 ) = 4 ( 1 + 2 + 1 ) = 4 ( 2 + 1 + 1 ) = 4. So in total, there are 3 ways.Input: N = 5 Output: 6 ( 1 + 1 + 3
4 min read
Print all possible K-length subsequences of first N natural numbers with sum N Given two positive integers N and K, the task is to print all possible K-length subsequences from first N natural numbers whose sum of elements is equal to N. Examples: Input: N = 5, K = 3 Output: { {1, 1, 3}, {1, 2, 2}, {1, 3, 1}, {2, 1, 2}, {2, 2, 1}, {3, 1, 1} } Explanation: 1 + 1 + 3 = N(= 5) an
9 min read
Sum of first N natural numbers which are not powers of K Given two integers n and k , the task is to find the sum of all the numbers within the range [1, n] excluding the numbers which are positive powers of k i.e. the numbers k, k2, k3 and so on.Examples: Input: n = 10, k = 3 Output: 43 1 + 2 + 4 + 5 + 6 + 7 + 8 + 10 = 43 3 and 9 are excluded as they are
9 min read