- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Number of arrays of size N whose elements are positive integers and sum is K in C++

We are given two numbers n and k. We need to find the count of arrays that can be formed using the n numbers whose sum is k.

The number of arrays of size N with sum K is $\dbinom{k - 1}{n - 1}$.

This a straightforward formula to find the number arrays that can be formed using n elements whose sum k. Let's see an example.

**Input**

n = 1 k = 2

**Output**

1

The only array that can be formed is [2]

**Input**

n = 2 k = 4

**Output**

3

The arrays that can be formed are [1, 3], [2, 2], [3, 1].

## Algorithm

- Initialise the numbers n and k.
- Write a function to calculate the factorial of a number.
- Now, write our main function to compute the binomial as seen above.
- Return the answer.

## Implementation

Following is the implementation of the above algorithm in C++

#include <bits/stdc++.h> using namespace std; int factorial(int n) { int result = 1; for (int i = 2; i <= n; i++) { result *= i; } return result; } int getNumberOfArraysCount(int n, int k) { return factorial(n) / (factorial(k) * factorial(n - k)); } int main() { int N = 5, K = 8; cout << getNumberOfArraysCount(K - 1, N - 1) << endl; return 0; }

## Output

If you run the above code, then you will get the following result.

35

- Related Questions & Answers
- n-th number whose sum of digits is ten in C++
- Number of pairs from the first N natural numbers whose sum is divisible by K in C++
- Find the Minimum Number of Fibonacci Numbers Whose Sum Is K in C++
- Ways to write N as sum of two or more positive integers in C++
- Find M-th number whose repeated sum of digits of a number is N in C++
- Program to count number of consecutive lists whose sum is n in C++
- Find number of substrings of length k whose sum of ASCII value of characters is divisible by k in C++
- Program to count number of paths whose sum is k in python
- Maximum sum of increasing order elements from n arrays in C++
- Count of elements whose absolute difference with the sum of all the other elements is greater than k in C++
- Minimum number of squares whose sum equals to given number n
- Program to find number of pairs from N natural numbers whose sum values are divisible by k in Python
- Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold in C++
- Minimum number of single digit primes required whose sum is equal to N in C++
- Maximum sum of increasing order elements from n arrays in C++ program

Advertisements