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.

**Examples:**

Input : N = 2, K = 3 Output : 2Explanation:[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 – 1}C_{N – 1}.

Below is implementation of this approach:

## C++

`// CPP Program to find the number of arrays of ` `// size N whose elements are positive integers ` `// and sum is K ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Return nCr ` `int` `binomialCoeff(` `int` `n, ` `int` `k) ` `{ ` ` ` `int` `C[k + 1]; ` ` ` `memset` `(C, 0, ` `sizeof` `(C)); ` ` ` ` ` `C[0] = 1; ` `// nC0 is 1 ` ` ` ` ` `for` `(` `int` `i = 1; i <= n; i++) { ` ` ` `// Compute next row of pascal triangle using ` ` ` `// the previous row ` ` ` `for` `(` `int` `j = min(i, k); j > 0; j--) ` ` ` `C[j] = C[j] + C[j - 1]; ` ` ` `} ` ` ` `return` `C[k]; ` `} ` ` ` `// Return the number of array that can be ` `// formed of size n and sum equals to k. ` `int` `countArray(` `int` `N, ` `int` `K) ` `{ ` ` ` `return` `binomialCoeff(K - 1, N - 1); ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `N = 2, K = 3; ` ` ` ` ` `cout << countArray(N, K) << endl; ` ` ` ` ` `return` `0; ` `} ` |

## Java

`// Java Program to find the ` `// number of arrays of size ` `// N whose elements are positive ` `// integers and sum is K ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Return nCr ` `static` `int` `binomialCoeff(` `int` `n, ` ` ` `int` `k) ` `{ ` ` ` `int` `[]C = ` `new` `int` `[k + ` `1` `]; ` ` ` ` ` ` ` `C[` `0` `] = ` `1` `; ` `// nC0 is 1 ` ` ` ` ` `for` `(` `int` `i = ` `1` `; i <= n; i++) ` ` ` `{ ` ` ` `// Compute next row of pascal ` ` ` `// triangle using the previous row ` ` ` `for` `(` `int` `j = Math.min(i, k); j > ` `0` `; j--) ` ` ` `C[j] = C[j] + C[j - ` `1` `]; ` ` ` `} ` ` ` `return` `C[k]; ` `} ` ` ` `// Return the number of ` `// array that can be ` `// formed of size n and ` `// sum equals to k. ` `static` `int` `countArray(` `int` `N, ` `int` `K) ` `{ ` ` ` `return` `binomialCoeff(K - ` `1` `, N - ` `1` `); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` `int` `N = ` `2` `, K = ` `3` `; ` ` ` `System.out.println( countArray(N, K)); ` `} ` `} ` ` ` `// This code is contributed by anuj_67. ` |

## C#

`// C# Program to find the ` `// number of arrays of size ` `// N whose elements are positive ` `// integers and sum is K ` `using` `System; ` ` ` `class` `GFG ` `{ ` `// Return nCr ` `static` `int` `binomialCoeff(` `int` `n, ` ` ` `int` `k) ` `{ ` ` ` `int` `[]C = ` `new` `int` `[k + 1]; ` ` ` ` ` ` ` `C[0] = 1; ` `// nC0 is 1 ` ` ` ` ` `for` `(` `int` `i = 1; i <= n; i++) ` ` ` `{ ` ` ` `// Compute next row of ` ` ` `// pascal triangle using ` ` ` `// the previous row ` ` ` `for` `(` `int` `j = Math.Min(i, k); ` ` ` `j > 0; j--) ` ` ` `C[j] = C[j] + C[j - 1]; ` ` ` `} ` ` ` `return` `C[k]; ` `} ` ` ` `// Return the number of ` `// array that can be ` `// formed of size n and ` `// sum equals to k. ` `static` `int` `countArray(` `int` `N, ` ` ` `int` `K) ` `{ ` ` ` `return` `binomialCoeff(K - 1, ` ` ` `N - 1); ` `} ` ` ` `// Driver Code ` `static` `public` `void` `Main () ` `{ ` ` ` `int` `N = 2, K = 3; ` ` ` ` ` `Console.WriteLine( ` ` ` `countArray(N, K)); ` `} ` `} ` ` ` `// This code is contributed by ajit ` |

## PHP

`<?php ` `// PHP Program to find the ` `// number of arrays of size ` `// N whose elements are ` `// positive integers and ` `// sum is K ` ` ` `// Return nCr ` `function` `binomialCoeff(` `$n` `, ` `$k` `) ` `{ ` ` ` `$C` `= ` `array_fill` `(0, (` `$k` `+ 1), 0); ` ` ` ` ` `$C` `[0] = 1; ` `// nC0 is 1 ` ` ` ` ` `for` `(` `$i` `= 1; ` `$i` `<= ` `$n` `; ` `$i` `++) ` ` ` `{ ` ` ` `// Compute next row ` ` ` `// of pascal triangle ` ` ` `// using the previous row ` ` ` `for` `(` `$j` `= min(` `$i` `, ` `$k` `); ` ` ` `$j` `> 0; ` `$j` `--) ` ` ` `$C` `[` `$j` `] = ` `$C` `[` `$j` `] + ` ` ` `$C` `[` `$j` `- 1]; ` ` ` `} ` ` ` `return` `$C` `[` `$k` `]; ` `} ` ` ` `// Return the number of ` `// array that can be ` `// formed of size n and ` `// sum equals to k. ` `function` `countArray(` `$N` `, ` `$K` `) ` `{ ` ` ` `return` `binomialCoeff(` `$K` `- 1, ` ` ` `$N` `- 1); ` `} ` ` ` `// Driver Code ` `$N` `= 2; ` `$K` `= 3; ` ` ` `echo` `countArray(` `$N` `, ` `$K` `); ` ` ` `// This code is contributed by mits ` `?> ` |

**Output:**

2

## Recommended Posts:

- Minimum splits in a binary string such that every substring is a power of 4 or 6.
- Check if a binary string contains all permutations of length k
- Two odd occurring elements in an array where all other occur even times
- Pair formation such that maximum pair sum is minimized
- Construct array having X subsequences with maximum difference smaller than d
- Maximum adjacent difference in an array in its sorted form
- Prefix Sum of Matrix (Or 2D Array)
- Combinatorics on ordered trees
- Rearrange an array such that 'arr[j]' becomes 'i' if 'arr[i]' is 'j' | Set 1
- Rearrange positive and negative numbers in O(n) time and O(1) extra space
- Check if an array is Wave Array
- Multidimensional Arrays in Java
- Minimum increment in the sides required to get non-negative area of a triangle
- Program to check if an array is palindrome or not using Recursion
- Sort an array of pairs using Java Pair and Comparator

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.