Find number of solutions of a linear equation of n variables (original) (raw)
Last Updated : 16 Dec, 2022
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 = 3, y = 1". It may be assumed that all coefficients in given equation are positive integers. Example :
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 implementation of above solution.
C++ `
// 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 that 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;
// Initialize count
// of solutions
int result = 0;
// One by subtract all smaller or
// equal coefficients and recur
for (int i = start; i <= end; i++)
if (coeff[i] <= rhs)
result += countSol(coeff, i, end,
rhs - coeff[i]);
return result;
}
// Driver Code
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;
}
Java
// A naive recursive Java program
// to find number of non-negative
// solutions for a given linear equation
import java.io.*;
class GFG
{
// Recursive function that returns
// count of solutions for given
// rhs value and coefficients coeff[start..end]
static int countSol(int coeff[], int start,
int end, int rhs)
{
// Base case
if (rhs == 0)
return 1;
// Initialize count of solutions
int result = 0;
// One by subtract all smaller or
// equal coefficients and recur
for (int i = start; i <= end; i++)
if (coeff[i] <= rhs)
result += countSol(coeff, i, end,
rhs - coeff[i]);
return result;
}
// Driver Code
public static void main (String[] args)
{
int coeff[] = {2, 2, 5};
int rhs = 4;
int n = coeff.length;
System.out.println (countSol(coeff, 0,
n - 1, rhs));
}
}
// This code is contributed by vt_m.
Python3
A naive recursive Python program
to find number of non-negative
solutions for a given linear equation
Recursive function that returns
count of solutions for given rhs
value and coefficients coeff[stat...end]
def countSol(coeff, start, end, rhs):
# Base case
if (rhs == 0):
return 1
# Initialize count of solutions
result = 0
# One by one subtract all smaller or
# equal coefficients and recur
for i in range(start, end+1):
if (coeff[i] <= rhs):
result += countSol(coeff, i, end,
rhs - coeff[i])
return result
Driver Code
coeff = [2, 2, 5]
rhs = 4
n = len(coeff)
print(countSol(coeff, 0, n - 1, rhs))
This code is contributed
by Soumen Ghosh
C#
// A naive recursive C# program
// to find number of non-negative
// solutions for a given linear equation
using System;
class GFG
{
// Recursive function that
// returns count of solutions
// for given RHS value and
// coefficients coeff[start..end]
static int countSol(int []coeff, int start,
int end, int rhs)
{
// Base case
if (rhs == 0)
return 1;
// Initialize count of solutions
int result = 0;
// One by subtract all smaller or
// equal coefficients and recur
for (int i = start; i <= end; i++)
if (coeff[i] <= rhs)
result += countSol(coeff, i, end,
rhs - coeff[i]);
return result;
}
// Driver Code
public static void Main ()
{
int []coeff = {2, 2, 5};
int rhs = 4;
int n = coeff.Length;
Console.Write (countSol(coeff, 0,
n - 1, rhs));
}
}
// This Code is contributed
// by nitin mittal.
PHP
start,start, start,end, $rhs)
{
// Base case
if ($rhs == 0)
return 1;
// Initialize count of solutions
$result = 0;
// One by subtract all smaller or
// equal coefficients and recur
for ($i = start;start; start;i <= end;end; end;i++)
if ($coeff[$i] <= $rhs)
result+=countSol(result += countSol(result+=countSol(coeff, i,i, i,end,
rhs−rhs - rhs−coeff[$i]);
return $result;
}
// Driver Code
$coeff = array (2, 2, 5);
$rhs = 4; n=sizeof(n = sizeof(n=sizeof(coeff);
echo countSol($coeff, 0, n−1,n - 1, n−1,rhs);
// This code is contributed by ajit
?>
JavaScript
`
Output :
3
Time Complexity: O(2^n)
Auxiliary Space: O(2^n) , because of recursive calls
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 implementation.
C++ `
// 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 count 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 Code
int main()
{
int coeff[] = {2, 2, 5};
int rhs = 4;
int n = sizeof(coeff) / sizeof(coeff[0]);
cout << countSol(coeff, n, rhs);
return 0;
}
Java
// A Dynamic programming based Java program
// to find number of non-negative solutions
// for a given linear equation
import java.util.Arrays;
class GFG
{
// Returns count of solutions for given
// rhs and coefficients coeff[0..n-1]
static int countSol(int coeff[],
int n, int rhs)
{
// Create and initialize a table to
// store results of subproblems
int dp[] = new int[rhs + 1];
Arrays.fill(dp, 0);
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 code
public static void main (String[] args)
{
int coeff[] = {2, 2, 5};
int rhs = 4;
int n = coeff.length;
System.out.print(countSol(coeff, n, rhs));
}
}
// This code is contributed by Anant Agarwal
Python3
A Dynamic Programming based
Python program to find number
of non-negative solutions for
a given linear equation
Returns count of solutions for given
rhs and coefficients coeff[0...n-1]
def countSol(coeff, n, rhs):
# Create and initialize a table
# to store results of subproblems
dp = [0 for i in range(rhs + 1)]
dp[0] = 1
# Fill table in bottom up manner
for i in range(n):
for j in range(coeff[i], rhs + 1):
dp[j] += dp[j - coeff[i]]
return dp[rhs]
Driver Code
coeff = [2, 2, 5]
rhs = 4
n = len(coeff)
print(countSol(coeff, n, rhs))
This code is contributed
by Soumen Ghosh
C#
// A Dynamic programming based
// C# program to find number of
// non-negative solutions for a
// given linear equation
using System;
class GFG
{
// Returns count of solutions
// for given rhs and coefficients
// coeff[0..n-1]
static int countSol(int []coeff,
int n, int rhs)
{
// Create and initialize a
// table to store results
// of subproblems
int []dp = new int[rhs + 1];
// Arrays.fill(dp, 0);
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 code
public static void Main ()
{
int []coeff = {2, 2, 5};
int rhs = 4;
int n = coeff.Length;
Console.Write(countSol(coeff,
n, rhs));
}
}
// This code is contributed
// by shiv_bhakt.
PHP
n,n, n,rhs)
{
// Create and initialize a table
// to store results of subproblems
$dp = str_repeat ("\0", 256);
$dp[0] = 1;
// Fill table in
// bottom up manner
for ($i = 0; i<i < i<n; $i++)
for ($j = coeff[coeff[coeff[i];
j<=j <= j<=rhs; $j++)
dp[dp[dp[j] = dp[dp[dp[j] + ($dp[$j -
coeff[coeff[coeff[i]]);
return dp[dp[dp[rhs];
}
// Driver Code
$coeff = array(2, 2, 5);
$rhs = 4;
// n=count(n = count(n=count(coeff); n=sizeof(n = sizeof(n=sizeof(coeff) / sizeof($coeff[0]);
echo countSol($coeff, n,n, n,rhs);
// This code is contributed
// by shiv_bhakt.
?>
JavaScript
`
Output :
3
Time Complexity: O(n * rhs)
Auxiliary Space: O(rhs) , because of the size of dp used.