Given a number n (number of variables) and val (sum of the variables), find out how many such nonnegative integral solutions are possible.
Examples :
Input : n = 5, val = 1 Output : 5 Explanation: x1 + x2 + x3 + x4 + x5 = 1 Number of possible solution are : (0 0 0 0 1), (0 0 0 1 0), (0 0 1 0 0), (0 1 0 0 0), (1 0 0 0 0) Total number of possible solutions are 5 Input : n = 5, val = 4 Output : 70 Explanation: x1 + x2 + x3 + x4 + x5 = 4 Number of possible solution are: (1 1 1 1 0), (1 0 1 1 1), (0 1 1 1 1), (2 1 0 0 1), (2 2 0 0 0)........ so on...... Total numbers of possible solutions are 70
Asked in: Microsoft Interview
1. Make a recursive function call to countSolutions(int n, int val)
2. Call this Solution function countSolutions(n-1, val-i) until n = 1 and val >=0 and then return 1.
Below is the implementation of above approach:
C++
// CPP program to find the numbers // of non negative integral solutions #include<iostream> using namespace std; // return number of non negative // integral solutions int countSolutions(int n, int val) { // initialize total = 0 int total = 0; // Base Case if n = 1 and val >= 0 // then it should return 1 if (n == 1 && val >=0) return 1; // iterate the loop till equal the val for (int i = 0; i <= val; i++){ // total solution of equations // and again call the recursive // function Solutions(variable,value) total += countSolutions(n-1, val-i); } // return the total no possible solution return total; } // driver code int main(){ int n = 5; int val = 20; cout<<countSolutions(n, val); } //This code is contributed by Smitha Dinesh Semwal |
Java
// Java program to find the numbers // of non negative integral solutions class GFG { // return number of non negative // integral solutions static int countSolutions(int n, int val) { // initialize total = 0 int total = 0; // Base Case if n = 1 and val >= 0 // then it should return 1 if (n == 1 && val >= 0) return 1; // iterate the loop till equal the val for (int i = 0; i <= val; i++) { // total solution of equations // and again call the recursive // function Solutions(variable, value) total += countSolutions(n - 1, val - i); } // return the total no possible solution return total; } // Driver code public static void main(String[] args) { int n = 5; int val = 20; System.out.print(countSolutions(n, val)); } } // This code is contributed by Anant Agarwal. |
Python3
# Python3 program to find the numbers # of non negative integral solutions # return number of non negative # integral solutions def countSolutions(n, val): # initialize total = 0 total = 0 # Base Case if n = 1 and val >= 0 # then it should return 1 if n == 1 and val >=0: return 1 # iterate the loop till equal the val for i in range(val+1): # total solution of equations # and again call the recursive # function Solutions(variable,value) total += countSolutions(n-1, val-i) # return the total no possible solution return total # driver code n = 5val = 20print(countSolutions(n, val)) |
C#
// C# program to find the numbers // of non negative integral solutions using System; class GFG { // return number of non negative // integral solutions static int countSolutions(int n, int val) { // initialize total = 0 int total = 0; // Base Case if n = 1 and val >= 0 // then it should return 1 if (n == 1 && val >= 0) return 1; // iterate the loop till equal the val for (int i = 0; i <= val; i++) { // total solution of equations // and again call the recursive // function Solutions(variable, value) total += countSolutions(n - 1, val - i); } // return the total no possible solution return total; } // Driver code public static void Main() { int n = 5; int val = 20; Console.WriteLine(countSolutions(n, val)); } } // This code is contributed by Anant vt_m. |
PHP
<?php // PHP program to find the numbers // of non negative integral solutions // return number of non negative // integral solutions function countSolutions($n, $val) { // initialize total = 0 $total = 0; // Base Case if n = 1 and // val >= 0 then it should // return 1 if ($n == 1 && $val >=0) return 1; // iterate the loop // till equal the val for ($i = 0; $i <= $val; $i++) { // total solution of equations // and again call the recursive // function Solutions(variable,value) $total += countSolutions($n - 1, $val - $i); } // return the total // no possible solution return $total; } // Driver Code $n = 5; $val = 20; echo countSolutions($n, $val); // This code is contributed by nitin mittal. ?> |
Output :
10626
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:
- Number of integral solutions of the equation x1 + x2 +.... + xN = k
- Number of integral solutions for equation x = b*(sumofdigits(x)^a)+c
- Number of non-negative integral solutions of a + b + c = n
- Find initial integral solution of Linear Diophantine equation if finite solution exists
- Find the integral roots of a given Cubic equation
- Find number of solutions of a linear equation of n variables
- Program to find number of solutions in Quadratic Equation
- Number of solutions for the equation x + y + z <= n
- Find the number of solutions to the given equation
- Python | Finding Solutions of a Polynomial Equation
- Number of Integral Points between Two Points
- Count Integral points inside a Triangle
- Trapezoidal Rule for Approximate Value of Definite Integral
- Program for finding the Integral of a given function using Boole's Rule
- Count of integral coordinates that lies inside a Square
- Find integral points with minimum distance from given set of integers using BFS
- Probability that an arbitrary positive divisor of 10^X is an integral multiple of 10^Y
- Count number of solutions of x^2 = 1 (mod p) in given range
- Number of solutions to Modular Equations
- Find 'N' number of solutions with the given inequality equations
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.

