![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 integral solutions of the equation x1 + x2 +…. + xN = k in C++
The solutions for the equation are
- The number of non-negative integral solutions of the equation are $\left(\begin{array}{c}n-k+1\ k\end{array}\right)$
- The number of positive integral solutions of the equation are $\left(\begin{array}{c}k-1\ n-1\end{array}\right)$
Add both to get the required answer. Let's see an example.
Input
n = 4 k = 7
Output
140
Algorithm
- Initialise the numbers n and k.
- Find the integral solutions of not-negative and positive numbers.
- Add both of them.
- 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 product = 1; for (int i = 2; i <= n; i++) { product *= i; } return product; } int nCr(int n, int r) { return factorial(n) / (factorial(n - r) * factorial(r)); } int main() { int n = 4; int k = 7; cout << nCr(n + k - 1, k) + nCr(k - 1, n - 1) &l<t; endl; return 0; }
Output
If you run the above code, then you will get the following result.
140
Advertisements