Number of non-negative integral solutions of sum equation
Last Updated :
29 Oct, 2023
Given a number n (number of variables) and val (sum of the variables), find out how many such non-negative 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++
#include<iostream>
using namespace std;
int countSolutions( int n, int val)
{
int total = 0;
if (n == 1 && val >=0)
return 1;
for ( int i = 0; i <= val; i++){
total += countSolutions(n-1, val-i);
}
return total;
}
int main(){
int n = 5;
int val = 20;
cout<<countSolutions(n, val);
}
|
Java
class GFG {
static int countSolutions( int n, int val) {
int total = 0 ;
if (n == 1 && val >= 0 )
return 1 ;
for ( int i = 0 ; i <= val; i++) {
total += countSolutions(n - 1 , val - i);
}
return total;
}
public static void main(String[] args) {
int n = 5 ;
int val = 20 ;
System.out.print(countSolutions(n, val));
}
}
|
Python3
def countSolutions(n, val):
total = 0
if n = = 1 and val > = 0 :
return 1
for i in range (val + 1 ):
total + = countSolutions(n - 1 , val - i)
return total
n = 5
val = 20
print (countSolutions(n, val))
|
C#
using System;
class GFG {
static int countSolutions( int n, int val) {
int total = 0;
if (n == 1 && val >= 0)
return 1;
for ( int i = 0; i <= val; i++) {
total += countSolutions(n - 1, val - i);
}
return total;
}
public static void Main()
{
int n = 5;
int val = 20;
Console.WriteLine(countSolutions(n, val));
}
}
|
Javascript
<script>
function countSolutions(n, val) {
let total = 0;
if (n == 1 && val >= 0)
return 1;
for (let i = 0; i <= val; i++) {
total += countSolutions(n - 1, val - i);
}
return total;
}
let n = 5;
let val = 20;
document.write(countSolutions(n, val));
</script>
|
PHP
<?php
function countSolutions( $n , $val )
{
$total = 0;
if ( $n == 1 && $val >=0)
return 1;
for ( $i = 0; $i <= $val ; $i ++)
{
$total += countSolutions( $n - 1,
$val - $i );
}
return $total ;
}
$n = 5;
$val = 20;
echo countSolutions( $n , $val );
?>
|
Output :
10626
Dynamic Programming Approach:
(Using Memoization)
C++
#include<bits/stdc++.h>
using namespace std;
int dp[1001][1001];
int countSolutions( int n, int val)
{
int total = 0;
if (n == 1 && val >=0) {
return 1;
}
if (dp[n][val] != -1) {
return dp[n][val];
}
for ( int i = 0; i <= val; i++){
total += countSolutions(n-1, val-i);
}
dp[n][val] = total;
return dp[n][val];
}
int main(){
int n = 5;
int val = 20;
memset (dp, -1, sizeof (dp));
cout << countSolutions(n, val);
}
|
Java
import java.util.*;
public class GFG
{
static int dp[][] = new int [ 1001 ][ 1001 ];
static int countSolutions( int n, int val)
{
int total = 0 ;
if (n == 1 && val >= 0 ) {
return 1 ;
}
if (dp[n][val] != - 1 ) {
return dp[n][val];
}
for ( int i = 0 ; i <= val; i++){
total += countSolutions(n- 1 , val-i);
}
dp[n][val] = total;
return dp[n][val];
}
public static void main(String args[]){
int n = 5 ;
int val = 20 ;
for ( int i = 0 ; i < 1001 ; i++) {
for ( int j = 0 ; j < 1001 ; j++) {
dp[i][j]=- 1 ;
}
}
System.out.println(countSolutions(n, val));
}
}
|
Python3
dp = [[ - 1 for i in range ( 1001 )]
for j in range ( 1001 )]
def countSolutions(n, val):
total = 0
if n = = 1 and val > = 0 :
return 1
if (dp[n][val] ! = - 1 ):
return dp[n][val]
for i in range (val + 1 ):
total + = countSolutions(n - 1 , val - i)
dp[n][val] = total
return dp[n][val]
n = 5
val = 20
print (countSolutions(n, val))
|
C#
using System;
class GFG
{
static int [,]dp = new int [1001, 1001];
static int countSolutions( int n, int val)
{
int total = 0;
if (n == 1 && val >=0) {
return 1;
}
if (dp[n, val] != -1) {
return dp[n, val];
}
for ( int i = 0; i <= val; i++){
total += countSolutions(n-1, val-i);
}
dp[n, val] = total;
return dp[n, val];
}
public static void Main(){
int n = 5;
int val = 20;
for ( int i = 0; i < 1001; i++) {
for ( int j = 0; j < 1001; j++) {
dp[i, j] = -1;
}
}
Console.Write(countSolutions(n, val));
}
}
|
Javascript
<script>
var dp = new Array(1001);
for ( var i = 0; i < dp.length; i++) {
dp[i] = new Array(1001);
}
function countSolutions(n, val) {
let total = 0;
if (n == 1 && val >= 0)
return 1;
if (dp[n][val] != -1)
return dp[n][val];
for (let i = 0; i <= val; i++) {
total += countSolutions(n - 1, val - i);
}
return dp[n][val] = total;
}
let n = 5;
let val = 20;
for (let i = 0; i < 1001; i++) {
for (let j = 0; j < 1001; j++) {
dp[i][j] = -1;
}
}
document.write(countSolutions(n, val));
</script>
|
Time Complexity: O(n * val)
Auxiliary Space: O(n * val)
Dynamic Programming Approach: (Bottom-Up)
The approach used here is a bottom-up dynamic programming approach. We start with the simplest case (one variable) and build up the solution to the n variables.
- We create a two-dimensional array called dp with dimensions n+1 x val+1, where dp[i][j] represents the number of solutions to the equation with i variables and sum j.
- Since there is only one variable in the first row (i=1), the number of solutions to the equation is always 1. Thus, initialize the first row of dp with all 1’s.
- For each subsequent row (i > 1) and each column j, we calculate dp[i][j] as the sum of dp[i-1][j-k] for all possible values of k between 0 and j. Because, for each solution to the equation with i variables and sum j, we can obtain a solution to the equation with i-1 variables and sum j-k by subtracting k from one of the variables.
- After computing all values of dp[n][j], we return dp[n][val], which represents the number of non-negative integral solutions to the equation with n variables and sum val.
Here is the Java Implementation of above algorithm
C++
#include <iostream>
using namespace std;
int Solutions( int n, int val)
{
int dp[n + 1][val + 1];
for ( int i = 0; i <= val; i++) {
dp[1][i] = 1;
}
for ( int i = 2; i <= n; i++) {
for ( int j = 0; j <= val; j++) {
dp[i][j] = 0;
for ( int k = 0; k <= j; k++) {
dp[i][j] += dp[i - 1][j - k];
}
}
}
return dp[n][val];
}
int main()
{
int n = 5;
int val = 20;
cout << Solutions(n, val) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int countSolutions( int n, int val)
{
int dp[][] = new int [n + 1 ][val + 1 ];
for ( int i = 0 ; i <= val; i++) {
dp[ 1 ][i] = 1 ;
}
for ( int i = 2 ; i <= n; i++) {
for ( int j = 0 ; j <= val; j++) {
dp[i][j] = 0 ;
for ( int k = 0 ; k <= j; k++) {
dp[i][j] += dp[i - 1 ][j - k];
}
}
}
return dp[n][val];
}
public static void main(String[] args)
{
int n = 5 ;
int val = 20 ;
System.out.println(countSolutions(n, val));
}
}
|
Python3
def solutions(n, val):
dp = [[ 0 for _ in range (val + 1 )] for _ in range (n + 1 )]
for i in range (val + 1 ):
dp[ 1 ][i] = 1
for i in range ( 2 , n + 1 ):
for j in range (val + 1 ):
dp[i][j] = 0
for k in range (j + 1 ):
dp[i][j] + = dp[i - 1 ][j - k]
return dp[n][val]
if __name__ = = "__main__" :
n = 5
val = 20
print (solutions(n, val))
|
C#
using System;
class GFG
{
public static int CountSolutions( int n, int val)
{
int [,] dp = new int [n + 1, val + 1];
for ( int i = 0; i <= val; i++)
{
dp[1, i] = 1;
}
for ( int i = 2; i <= n; i++)
{
for ( int j = 0; j <= val; j++)
{
dp[i, j] = 0;
for ( int k = 0; k <= j; k++)
{
dp[i, j] += dp[i - 1, j - k];
}
}
}
return dp[n, val];
}
public static void Main( string [] args)
{
int n = 5;
int val = 20;
Console.WriteLine(CountSolutions(n, val));
}
}
|
Javascript
function countSolutions(n, val)
{
const dp = new Array(n + 1).fill( null ).map(() => new Array(val + 1).fill(0));
for (let i = 0; i <= val; i++) {
dp[1][i] = 1;
}
for (let i = 2; i <= n; i++) {
for (let j = 0; j <= val; j++) {
dp[i][j] = 0;
for (let k = 0; k <= j; k++) {
dp[i][j] += dp[i - 1][j - k];
}
}
}
return dp[n][val];
}
const n = 5;
const val = 20;
console.log(countSolutions(n, val));
|
Complexity Analysis
Time Complexity : O(n * val^2), where n is the number of variables and val is the given sum.
Auxiliary Space : O( n * val), creating a 2D array of size (n + 1) * (val + 1).
Share your thoughts in the comments
Please Login to comment...