# Find number of solutions of a linear equation of n variables

Given a linear equation of n variables, find number of non-negative integer solutions of it. For example,let the given equation be “x + 2y = 5”, solutions of this equation are “x = 1, y = 2”, “x = 5, y = 0” and “x = 1. It may be assumed that all coefficients in given equation are positive integers.

Example:

```Input:  coeff[] = {1, 2}, rhs = 5
Output: 3
The equation "x + 2y = 5" has 3 solutions.
(x=3,y=1), (x=1,y=2), (x=5,y=0)

Input:  coeff[] = {2, 2, 3}, rhs = 4
Output: 3
The equation "2x + 2y + 3z = 4"  has 3 solutions.
(x=0,y=2,z=0), (x=2,y=0,z=0), (x=1,y=1,z=0)
```

We strongly recommend you to minimize your browser and try this yourself first.

We can solve this problem recursively. The idea is to subtract first coefficient from rhs and then recur for remaining value of rhs.

```If rhs = 0
countSol(coeff, 0, rhs, n-1) = 1
Else
countSol(coeff, 0, rhs, n-1) = ∑countSol(coeff, i, rhs-coeff[i], m-1)
where coeff[i]<=rhs and
i varies from 0 to n-1
```

Below is recursive C++ implementation of above solution.

```// A naive recursive C++ program to find number of non-negative
//  solutions for a given linear equation
#include<bits/stdc++.h>
using namespace std;

// Recursive function thet returns count of solutions for given
// rhs value and coefficients coeff[start..end]
int countSol(int coeff[], int start, int end, int rhs)
{
// Base case
if (rhs == 0)
return 1;

int result = 0;  // Initialize count of solutions

// One by subtract all smaller or equal coefficiants and recur
for (int i=start; i<=end; i++)
if (coeff[i] <= rhs)
result += countSol(coeff, i, end, rhs-coeff[i]);

return result;
}

// Driver program
int main()
{
int coeff[]  = {2, 2, 5};
int rhs = 4;
int n = sizeof(coeff)/sizeof(coeff[0]);
cout << countSol(coeff, 0, n-1, rhs);
return 0;
}
```

Output:

`3`

The time complexity of above solution is exponential. We can solve this problem in Pseudo Polynomial Time (time complexity is dependent on numeric value of input) using Dynamic Programming. The idea is similar to Dynamic Programming solution Subset Sum problem. Below is Dynamic Programming based C++ implementation.

```// A Dynamic programming based C++ program to find number of
// non-negative solutions for a given linear equation
#include<bits/stdc++.h>
using namespace std;

// Returns counr of solutions for given rhs and coefficients
// coeff[0..n-1]
int countSol(int coeff[], int n, int rhs)
{
// Create and initialize a table to store results of
// subproblems
int dp[rhs+1];
memset(dp, 0, sizeof(dp));
dp[0] = 1;

// Fill table in bottom up manner
for (int i=0; i<n; i++)
for (int j=coeff[i]; j<=rhs; j++)
dp[j] += dp[j-coeff[i]];

return dp[rhs];
}

// Driver program
int main()
{
int coeff[]  = {2, 2, 5};
int rhs = 4;
int n = sizeof(coeff)/sizeof(coeff[0]);
cout << countSol(coeff, n, rhs);
return 0;
}
```

Output:

`3`

Time Complexity of above solution is O(n * rhs)

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
3.6 Average Difficulty : 3.6/5.0
Based on 31 vote(s)

Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.