Given N and K. The task is to find out how many different ways are there to represent N as the sum of K non-zero integers.
Examples:
Input: N = 5, K = 3
Output: 6
The possible combinations of integers are:
( 1, 1, 3 )
( 1, 3, 1 )
( 3, 1, 1 )
( 1, 2, 2 )
( 2, 2, 1 )
( 2, 1, 2 )Input: N = 10, K = 4
Output: 84
The approach to the problem is to observe a sequence and use combinations to solve the problem. To obtain a number N, N 1’s are required, summation of N 1’s will give N. The problem allows to use K integers only to make N.
Observation :
Let's take N = 5 and K = 3, then all
possible combinations of K numbers are: ( 1, 1, 3 )
( 1, 3, 1 )
( 3, 1, 1 )
( 1, 2, 2 )
( 2, 2, 1 )
( 2, 1, 2 )
The above can be rewritten as: ( 1, 1, 1 + 1 + 1 )
( 1, 1 + 1 + 1, 1 )
( 1 + 1 + 1, 1, 1 )
( 1, 1 + 1, 1 + 1 )
( 1 + 1, 1 + 1, 1 )
( 1 + 1, 1, 1 + 1 )
From above, a conclusion can be drawn that of N 1’s, k-1 commas have to be placed in between N 1’s and the remaining places are to be filled with ‘+’ signs. All combinations of placing k-1 commas and placing ‘+’ signs in the remaining places will be the answer. So, in general, for N there will be N-1 spaces between all 1, and out of those choose k-1 and place a comma in between those 1. In between the rest 1’s, place ‘+’ signs. So ways of choosing K-1 objects out of N-1 is
. The dynamic programming approach is used to calculate
.
Below is the implementation of the above approach:
C++
// CPP program to calculate Different ways to // represent N as sum of K non-zero integers. #include <bits/stdc++.h> using namespace std; // Returns value of Binomial Coefficient C(n, k) int binomialCoeff(int n, int k) { int C[n + 1][k + 1]; int i, j; // Calculate value of Binomial Coefficient in bottom up manner for (i = 0; i <= n; i++) { for (j = 0; j <= min(i, k); j++) { // Base Cases if (j == 0 || j == i) C[i][j] = 1; // Calculate value using previously stored values else C[i][j] = C[i - 1][j - 1] + C[i - 1][j]; } } return C[n][k]; } // Driver Code int main() { int n = 5, k = 3; cout << "Total number of different ways are " << binomialCoeff(n - 1, k - 1); return 0; } |
Java
// Java program to calculate // Different ways to represent // N as sum of K non-zero integers. import java.io.*; class GFG { // Returns value of Binomial // Coefficient C(n, k) static int binomialCoeff(int n, int k) { int C[][] = new int [n + 1][k + 1]; int i, j; // Calculate value of Binomial // Coefficient in bottom up manner for (i = 0; i <= n; i++) { for (j = 0; j <= Math.min(i, k); j++) { // Base Cases if (j == 0 || j == i) C[i][j] = 1; // Calculate value using // previously stored values else C[i][j] = C[i - 1][j - 1] + C[i - 1][j]; } } return C[n][k]; } // Driver Code public static void main (String[] args) { int n = 5, k = 3; System.out.println( "Total number of " + "different ways are " + binomialCoeff(n - 1, k - 1)); } } // This code is contributed // by anuj_67. |
Python3
# python 3 program to calculate Different ways to # represent N as sum of K non-zero integers. # Returns value of Binomial Coefficient C(n, k) def binomialCoeff(n, k): C = [[0 for i in range(k+1)]for i in range(n+1)] # Calculate value of Binomial Coefficient in bottom up manner for i in range(0,n+1,1): for j in range(0,min(i, k)+1,1): # Base Cases if (j == 0 or j == i): C[i][j] = 1 # Calculate value using previously stored values else: C[i][j] = C[i - 1][j - 1] + C[i - 1][j] return C[n][k] # Driver Code if __name__ == '__main__': n = 5 k = 3 print("Total number of different ways are",binomialCoeff(n - 1, k - 1)) # This code is contributed by # Sanjit_Prasad |
C#
// C# program to calculate // Different ways to represent // N as sum of K non-zero integers. using System; class GFG { // Returns value of Binomial // Coefficient C(n, k) static int binomialCoeff(int n, int k) { int [,]C = new int [n + 1, k + 1]; int i, j; // Calculate value of // Binomial Coefficient // in bottom up manner for (i = 0; i <= n; i++) { for (j = 0; j <= Math.Min(i, k); j++) { // Base Cases if (j == 0 || j == i) C[i, j] = 1; // Calculate value using // previously stored values else C[i, j] = C[i - 1, j - 1] + C[i - 1, j]; } } return C[n,k]; } // Driver Code public static void Main () { int n = 5, k = 3; Console.WriteLine( "Total number of " + "different ways are " + binomialCoeff(n - 1, k - 1)); } } // This code is contributed // by anuj_67. |
PHP
<?php // PHP program to calculate // Different ways to represent // N as sum of K non-zero integers. // Returns value of Binomial // Coefficient C(n, k) function binomialCoeff($n, $k) { $C = array(array()); $i; $j; // Calculate value of Binomial // Coefficient in bottom up manner for ($i = 0; $i <= $n; $i++) { for ($j = 0; $j <= min($i, $k); $j++) { // Base Cases if ($j == 0 or $j == $i) $C[$i][$j] = 1; // Calculate value using // previously stored values else $C[$i][$j] = $C[$i - 1][$j - 1] + $C[$i - 1][$j]; } } return $C[$n][$k]; } // Driver Code $n = 5; $k = 3; echo "Total number of " , "different ways are " , binomialCoeff($n - 1, $k - 1); // This code is contributed // by anuj_67. ?> |
Total number of different ways are 6
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Represent (2 / N) as the sum of three distinct positive integers of the form (1 / m)
- Ways to represent a number as a sum of 1's and 2's
- Number of ways to represent a number as sum of k fibonacci numbers
- Number of distinct ways to represent a number as sum of K unique primes
- Count of ways to represent N as sum of a prime number and twice of a square
- Count of integers up to N which represent a Binary number
- Count ways to express 'n' as sum of odd integers
- Count ways to express even number ‘n’ as sum of even integers
- Ways to form an array having integers in given range such that total sum is divisible by 2
- Number of ways to write N as a sum of K non-negative integers
- Different ways to sum n using numbers greater than or equal to m
- Count of different ways to express N as the sum of 1, 3 and 4
- Represent a number as a sum of maximum possible number of Prime Numbers
- Represent N as sum of K even numbers
- Number of ways to form a heap with n distinct integers
- Ways to select one or more pairs from two different sets
- Ways of dividing a group into two halves such that two elements are in different groups
- Calculate Stirling numbers which represents the number of ways to arrange r objects around n different circles
- Number of ways to arrange K different objects taking N objects at a time
- Number of arrays of size N whose elements are positive integers and sum is K
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.

