Count of all possible combinations of K numbers that sums to N
Last Updated :
03 Jan, 2022
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 = 3
Output:15
Explanation: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, 4, 1}, {4, 1, 2}, {4, 2, 1}, {3, 1, 3}, {1, 3, 3}, {3, 3, 1}, {2, 2, 3}, {2, 3, 2}, {3, 2, 2}
Input: N = 5, K = 5
Output: 1
Explanation: {1, 1, 1, 1, 1} is the only combination.
Naive Approach: This problem can be solved using recursion and then memoising the result to improve time complexity. To solve this problem, follow the below steps:
- Create a function, say countWaysUtil which will accept four parameters that are N, K, sum, and dp. Here N is the sum that K elements are required to have, K is the number of elements consumed, sum is the sum accumulated till now and dp is the matrix to memoise the result. This function will give the number of ways to get the sum in K numbers.
- Now initially call countWaysUtil with arguments N, K, sum=0 and dp as a matrix filled with all -1.
- In each recursive call:
- Check for the base cases:
- If the sum is equal to N and K become 0, then return 1.
- If the sum exceeds N and K is still greater than 0, then return 0.
- Now, run a for loop from 1 to N, to check the result for each outcome.
- Sum all the results in a variable cnt and return cnt after memoising.
- Print the answer according to the above observation.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int countWaysUtil( int N, int K, int sum,
vector<vector< int > >& dp)
{
if (sum == N and K == 0) {
return 1;
}
if (sum >= N and K >= 0) {
return 0;
}
if (K < 0) {
return 0;
}
if (dp[sum][K] != -1) {
return dp[sum][K];
}
int cnt = 0;
for ( int i = 1; i <= N; i++) {
cnt += countWaysUtil(
N, K - 1,
sum + i, dp);
}
return dp[sum][K] = cnt;
}
void countWays( int N, int K)
{
vector<vector< int > > dp(N + 1,
vector< int >(
K + 1, -1));
cout << countWaysUtil(N, K, 0, dp);
}
int main()
{
int N = 7, K = 3;
countWays(N, K);
}
|
Java
class GFG {
static int countWaysUtil( int N, int K, int sum,
int [][] dp)
{
if (sum == N && K == 0 ) {
return 1 ;
}
if (sum >= N && K >= 0 ) {
return 0 ;
}
if (K < 0 ) {
return 0 ;
}
if (dp[sum][K] != - 1 ) {
return dp[sum][K];
}
int cnt = 0 ;
for ( int i = 1 ; i <= N; i++) {
cnt += countWaysUtil(N, K - 1 , sum + i, dp);
}
return dp[sum][K] = cnt;
}
static void countWays( int N, int K)
{
int [][] dp = new int [N + 1 ][K + 1 ];
for ( int i = 0 ; i < N + 1 ; i++) {
for ( int j = 0 ; j < K + 1 ; j++) {
dp[i][j] = - 1 ;
}
}
System.out.print(countWaysUtil(N, K, 0 , dp));
}
public static void main(String[] args)
{
int N = 7 , K = 3 ;
countWays(N, K);
}
}
|
Python3
def countWaysUtil(N, K, sum , dp):
if ( sum = = N and K = = 0 ):
return 1
if ( sum > = N and K > = 0 ):
return 0
if (K < 0 ):
return 0
if (dp[ sum ][K] ! = - 1 ):
return dp[ sum ][K]
cnt = 0
for i in range ( 1 , N + 1 ):
cnt + = countWaysUtil(N, K - 1 , sum + i, dp)
dp[ sum ][K] = cnt
return dp[ sum ][K]
def countWays(N, K):
dp = [[ - 1 for _ in range (K + 1 )]
for _ in range (N + 1 )]
print (countWaysUtil(N, K, 0 , dp))
if __name__ = = "__main__" :
N = 7
K = 3
countWays(N, K)
|
C#
using System;
class GFG
{
static int countWaysUtil( int N, int K, int sum,
int [,]dp)
{
if (sum == N && K == 0) {
return 1;
}
if (sum >= N && K >= 0) {
return 0;
}
if (K < 0) {
return 0;
}
if (dp[sum, K] != -1) {
return dp[sum, K];
}
int cnt = 0;
for ( int i = 1; i <= N; i++) {
cnt += countWaysUtil(
N, K - 1,
sum + i, dp);
}
return dp[sum, K] = cnt;
}
static void countWays( int N, int K)
{
int [,]dp = new int [N + 1, K + 1];
for ( int i = 0; i < N + 1; i++) {
for ( int j = 0; j < K + 1; j++) {
dp[i, j] = -1;
}
}
Console.Write(countWaysUtil(N, K, 0, dp));
}
public static void Main()
{
int N = 7, K = 3;
countWays(N, K);
}
}
|
Javascript
<script>
function countWaysUtil(N, K, sum, dp) {
if (sum == N && K == 0) {
return 1;
}
if (sum >= N && K >= 0) {
return 0;
}
if (K < 0) {
return 0;
}
if (dp[sum][K] != -1) {
return dp[sum][K];
}
let cnt = 0;
for (let i = 1; i <= N; i++) {
cnt += countWaysUtil(
N, K - 1,
sum + i, dp);
}
return dp[sum][K] = cnt;
}
function countWays(N, K) {
let dp = new Array(N + 1).fill(0).map(() => new Array(K + 1).fill(-1))
document.write(countWaysUtil(N, K, 0, dp));
}
let N = 7, K = 3;
countWays(N, K);
</script>
|
Time Complexity: O(N*K)
Space Complexity: O(N*K)
Efficient Approach: This problem can also be solved using the binomial theorem. As the required sum is N with K elements, so suppose the K numbers are:
a1 + a2 + a3 + a4 + …….. + aK = N
According to the standard principle of partitioning in the binomial theorem, the above equation has a solution which is N+K-1CK-1, where K>=0.
But in our case, K>=1.
So, therefore N should be substituted with N-K and the equation becomes N-1CK-1
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int factorial( int n)
{
if (n == 0)
return 1;
return n * factorial(n - 1);
}
int totalWays( int N, int K)
{
if (N < K)
return 0;
int n1 = factorial(N - 1);
int n2 = factorial(K - 1) * factorial(N - K);
int ans = (n1 / n2);
return ans;
}
int main()
{
int N = 7;
int K = 3;
int ans = totalWays(N, K);
cout << ans;
return 0;
}
|
C
#include <stdio.h>
int factorial( int n)
{
if (n == 0)
return 1;
return n*factorial(n - 1);
}
int totalWays( int N, int K) {
if (N < K)
return 0;
int n1 = factorial(N - 1);
int n2 = factorial(K - 1)*factorial(N - K);
int ans = (n1/n2);
return ans;
}
int main()
{
int N = 7;
int K = 3;
int ans = totalWays(N, K);
printf ( "%d" ,ans);
return 0;
}
|
Java
class Solution{
static int factorial( int n)
{
if (n == 0 )
return 1 ;
return n*factorial(n - 1 );
}
static int totalWays( int N, int K) {
if (N < K)
return 0 ;
int n1 = factorial(N - 1 );
int n2 = factorial(K - 1 )*factorial(N - K);
int ans = (n1/n2);
return ans;
}
public static void main(String[] args)
{
int N = 7 ;
int K = 3 ;
int ans = totalWays(N, K);
System.out.println(ans);
}
}
|
Python3
from math import factorial
class Solution:
def totalWays( self , N, K):
if (N < K):
return 0
n1 = factorial(N - 1 )
n2 = factorial(K - 1 ) * factorial(N - K)
ans = (n1 / / n2)
return ans
if __name__ = = '__main__' :
N = 7
K = 3
ob = Solution()
ans = ob.totalWays(N, K)
print (ans)
|
C#
using System;
public class Solution {
static int factorial( int n) {
if (n == 0)
return 1;
return n * factorial(n - 1);
}
static int totalWays( int N, int K) {
if (N < K)
return 0;
int n1 = factorial(N - 1);
int n2 = factorial(K - 1) * factorial(N - K);
int ans = (n1 / n2);
return ans;
}
public static void Main(String[] args) {
int N = 7;
int K = 3;
int ans = totalWays(N, K);
Console.WriteLine(ans);
}
}
|
Javascript
<script>
function factorial(n)
{
if (n == 0)
return 1;
return n * factorial(n - 1);
}
function totalWays( N, K)
{
if (N < K)
return 0;
let n1 = factorial(N - 1);
let n2 = factorial(K - 1) * factorial(N - K);
let ans = (n1 / n2);
return ans;
}
let N = 7;
let K = 3;
let ans = totalWays(N, K);
document.write(ans);
</script>
|
Time complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...