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.
Input : N = 2, K = 3 Output : 2 Explanation: [1, 2] and [2, 1] are the only arrays of size 2 whose sum is 3. Input : n = 3, k = 7 Output : 15
Prerequisite: Stars and Bars
Suppose there are K identical objects which needs to be placed in N bins (N indices of the array) such that each bin have at least one object. Instead of starting to place objects into bins, we start placing the objects on a line, where the object for the first bin will be taken from the left, followed by the objects for the second bin, and so forth. Thus, the configuration will be determined once one knows what is the first object going to the second bin, and the first object going to the third bin, and so on. We can indicate this by placing N X 1 separating bars at some places between two objects; since no bin is allowed to be empty, there can be at most one bar between a given pair of objects. So, we have K objects in a line with K – 1 gaps. Now we have to choose N – 1 gaps to place bars from K – 1 gaps. This can be chosen by K – 1CN – 1.
Below is implementation of this approach:
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.
- Minimum product of k integers in an array of positive Integers
- Check whether a number can be represented as sum of K distinct positive integers
- Number of ways in which N can be represented as the sum of two positive integers
- Lexicographically smallest permutation of size A having B integers exceeding all preceeding integers
- Find the number of positive integers less than or equal to N that have an odd number of digits
- Only integer with positive value in positive negative value in array
- Ways to write n as sum of two or more positive integers
- Ways to write N as sum of two or more positive integers | Set-2
- Represent (2 / N) as the sum of three distinct positive integers of the form (1 / m)
- Find K distinct positive odd integers with sum N
- Print all possible ways to write N as sum of two or more positive integers
- Maximum number of distinct positive integers that can be used to represent N
- Count positive integers with 0 as a digit and maximum 'd' digits
- Longest alternating subsequence in terms of positive and negative integers
- Find Nth positive number whose digital root is X
- Nth positive number whose absolute difference of adjacent digits is at most 1
- Flip minimum signs of array elements to get minimum sum of positive elements possible
- Count 'd' digit positive integers with 0 as a digit
- Minimum product pair an array of positive Integers
- Longest sequence of positive integers in an array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.