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 :
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 implementation of above solution.
C++
#include<bits/stdc++.h>
using namespace std;
int countSol( int coeff[], int start,
int end, int rhs)
{
if (rhs == 0)
return 1;
int result = 0;
for ( int i = start; i <= end; i++)
if (coeff[i] <= rhs)
result += countSol(coeff, i, end,
rhs - coeff[i]);
return result;
}
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
import java.io.*;
class GFG
{
static int countSol( int coeff[], int start,
int end, int rhs)
{
if (rhs == 0 )
return 1 ;
int result = 0 ;
for ( int i = start; i <= end; i++)
if (coeff[i] <= rhs)
result += countSol(coeff, i, end,
rhs - coeff[i]);
return result;
}
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));
}
}
|
Python3
def countSol(coeff, start, end, rhs):
if (rhs = = 0 ):
return 1
result = 0
for i in range (start, end + 1 ):
if (coeff[i] < = rhs):
result + = countSol(coeff, i, end,
rhs - coeff[i])
return result
coeff = [ 2 , 2 , 5 ]
rhs = 4
n = len (coeff)
print (countSol(coeff, 0 , n - 1 , rhs))
|
C#
using System;
class GFG
{
static int countSol( int []coeff, int start,
int end, int rhs)
{
if (rhs == 0)
return 1;
int result = 0;
for ( int i = start; i <= end; i++)
if (coeff[i] <= rhs)
result += countSol(coeff, i, end,
rhs - coeff[i]);
return result;
}
public static void Main ()
{
int []coeff = {2, 2, 5};
int rhs = 4;
int n = coeff.Length;
Console.Write (countSol(coeff, 0,
n - 1, rhs));
}
}
|
PHP
<?php
function countSol( $coeff , $start , $end , $rhs )
{
if ( $rhs == 0)
return 1;
$result = 0;
for ( $i = $start ; $i <= $end ; $i ++)
if ( $coeff [ $i ] <= $rhs )
$result += countSol( $coeff , $i , $end ,
$rhs - $coeff [ $i ]);
return $result ;
}
$coeff = array (2, 2, 5);
$rhs = 4;
$n = sizeof( $coeff );
echo countSol( $coeff , 0, $n - 1, $rhs );
?>
|
Javascript
<script>
function countSol(coeff, start, end, rhs)
{
if (rhs == 0)
return 1;
let result = 0;
for (let i = start; i <= end; i++)
if (coeff[i] <= rhs)
result += countSol(coeff, i, end, rhs - coeff[i]);
return result;
}
let coeff = [2, 2, 5];
let rhs = 4;
let n = coeff.length;
document.write(countSol(coeff, 0, n - 1, rhs));
</script>
|
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++
#include<bits/stdc++.h>
using namespace std;
int countSol( int coeff[], int n, int rhs)
{
int dp[rhs + 1];
memset (dp, 0, sizeof (dp));
dp[0] = 1;
for ( int i = 0; i < n; i++)
for ( int j = coeff[i]; j <= rhs; j++)
dp[j] += dp[j - coeff[i]];
return dp[rhs];
}
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
import java.util.Arrays;
class GFG
{
static int countSol( int coeff[],
int n, int rhs)
{
int dp[] = new int [rhs + 1 ];
Arrays.fill(dp, 0 );
dp[ 0 ] = 1 ;
for ( int i = 0 ; i < n; i++)
for ( int j = coeff[i]; j <= rhs; j++)
dp[j] += dp[j - coeff[i]];
return dp[rhs];
}
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));
}
}
|
Python3
def countSol(coeff, n, rhs):
dp = [ 0 for i in range (rhs + 1 )]
dp[ 0 ] = 1
for i in range (n):
for j in range (coeff[i], rhs + 1 ):
dp[j] + = dp[j - coeff[i]]
return dp[rhs]
coeff = [ 2 , 2 , 5 ]
rhs = 4
n = len (coeff)
print (countSol(coeff, n, rhs))
|
C#
using System;
class GFG
{
static int countSol( int []coeff,
int n, int rhs)
{
int []dp = new int [rhs + 1];
dp[0] = 1;
for ( int i = 0; i < n; i++)
for ( int j = coeff[i]; j <= rhs; j++)
dp[j] += dp[j - coeff[i]];
return dp[rhs];
}
public static void Main ()
{
int []coeff = {2, 2, 5};
int rhs = 4;
int n = coeff.Length;
Console.Write(countSol(coeff,
n, rhs));
}
}
|
PHP
<?php
function countSol( $coeff , $n , $rhs )
{
$dp = str_repeat ( "\0" , 256);
$dp [0] = 1;
for ( $i = 0; $i < $n ; $i ++)
for ( $j = $coeff [ $i ];
$j <= $rhs ; $j ++)
$dp [ $j ] = $dp [ $j ] + ( $dp [ $j -
$coeff [ $i ]]);
return $dp [ $rhs ];
}
$coeff = array (2, 2, 5);
$rhs = 4;
$n = sizeof( $coeff ) / sizeof( $coeff [0]);
echo countSol( $coeff , $n , $rhs );
?>
|
Javascript
<script>
function countSol(coeff, n, rhs)
{
let dp = new Array(rhs + 1);
dp.fill(0);
dp[0] = 1;
for (let i = 0; i < n; i++)
for (let j = coeff[i]; j <= rhs; j++)
dp[j] += dp[j - coeff[i]];
return dp[rhs];
}
let coeff = [2, 2, 5];
let rhs = 4;
let n = coeff.length;
document.write(countSol(coeff, n, rhs));
</script>
|
Output :
3
Time Complexity: O(n * rhs)
Auxiliary Space: O(rhs) , because of the size of dp used.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
16 Dec, 2022
Like Article
Save Article