Given a number n (number of variables) and val (sum of the variables), find out how many such non negative integral solutions are possible.
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:
- 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
- Number of solutions for the equation x + y + z <= n
- Find the number of solutions to the given equation
- Program to find number of solutions in Quadratic Equation
- Find number of solutions of a linear equation of n variables
- Number of solutions of n = x + n ⊕ x
- Number of solutions for x < y, where a <= x <= b and c <= y <= d and x, y are integers
- Number of solutions to Modular Equations
- Count number of solutions of x^2 = 1 (mod p) in given range
- Find 'N' number of solutions with the given inequality equations
- Number of sextuplets (or six values) that satisfy an equation
- Number of ordered points pair satisfying line equation
- Number of Integral Points between Two Points
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.
Improved By : nitin mittal