Number of integral solutions of the equation x1 + x2 +…. + xN = k
Last Updated :
07 Mar, 2024
Given N and K. The task is to count the number of the integral solutions of a linear equation having N variable as given below:
x1 + x2+ x3…+ xN-1+…+xN = K
Examples:
Input: N = 3, K = 3
Output: 10
Explanation: Possible solutions are: (1,1,1),(1,0,2),(2,0,1),(1,2,0),(2,1,0),(0,1,2),(0,2,1),(3,0,0),(0,3,0),(0,0,3)
Input: N = 2, K = 2
Output: 3
Approach: This problem can be solved using the concept of Permutation and Combination. Below are the direct formulas for finding non-negative and positive integral solutions respectively.
Number of non-negative integral solutions of equation x1 + x2 + …… + xn = k is given by (n+k-1)! / (n-1)!*k!.
Number of positive integral solutions of equation x1 + x2 + ….. + xn = k is given by (k-1)! / (n-1)! * (k-n)!.
Note: Here that the non-negative integral solutions already include the positive integral solutions. Therefore, there is no need to add the number of positive integral solutions to the answer.
Below is the implementation of above approach:
c++
#include<iostream>
#define MAX 100
using namespace std ;
int nCr( int n, int r)
{
int fac[MAX] = {1} ;
for ( int i = 1 ; i < n + 1 ; i++)
{
fac[i] = fac[i - 1] * i ;
}
int ans = fac[n] / (fac[n - r] *
fac[r]) ;
return ans ;
}
int main()
{
int n = 3 ;
int k = 3 ;
int ans = nCr(n + k - 1 , k);
cout << ans ;
return 0 ;
}
|
Java
import java.io.*;
class GFG
{
static int nCr( int n, int r)
{
int fac[] = new int [ 100 ] ;
for ( int i = 0 ; i < n; i++)
fac[i] = 1 ;
for ( int i = 1 ; i < n + 1 ; i++)
{
fac[i] = fac[i - 1 ] * i ;
}
int ans = fac[n] / (fac[n - r] *
fac[r]);
return ans ;
}
public static void main (String[] args)
{
int n = 3 ;
int k = 3 ;
int ans = nCr(n + k - 1 , k);
System.out.println(ans) ;
}
}
|
C#
using System;
class GFG
{
static int nCr( int n, int r)
{
int [] fac = new int [100] ;
for ( int i = 0; i < n; i++)
fac[i] = 1;
for ( int i = 1 ; i < n + 1 ; i++)
{
fac[i] = fac[i - 1] * i ;
}
int ans = fac[n] / (fac[n - r] *
fac[r]);
return ans ;
}
public static void Main ()
{
int n = 3 ;
int k = 3 ;
int ans = nCr(n + k - 1 , k);
Console.Write(ans) ;
}
}
|
Javascript
<script>
function nCr(n, r)
{
var fac = Array(100).fill(1);
for ( var i = 1 ; i < n + 1 ; i++)
{
fac[i] = fac[i - 1] * i ;
}
var ans = fac[n] / (fac[n - r] *
fac[r]) ;
return ans ;
}
var n = 3 ;
var k = 3 ;
var ans = nCr(n + k - 1 , k);
document.write(ans );
</script>
|
PHP
<?php
function nCr( $n , $r )
{
$fac = array ();
array_push ( $fac , 1);
for ( $i = 1; $i < $n + 1; $i ++)
array_push ( $fac , $fac [ $i - 1] * $i );
$ans = $fac [ $n ] / ( $fac [ $n - $r ] *
$fac [ $r ]);
return $ans ;
}
$n = 3;
$k = 3;
$ans = nCr( $n + $k - 1, $k );
print ( $ans );
?>
|
Python3
def nCr(n, r):
fac = list ()
fac.append( 1 )
for i in range ( 1 , n + 1 ):
fac.append(fac[i - 1 ] * i)
ans = fac[n] / (fac[n - r] * fac[r])
return ans
n = 3
k = 3
ans = nCr(n + k - 1 , k)
print (ans)
|
Time Complexity: O(n)
Auxiliary Space: O(MAX)
Applications of the above concepts:
- Number of non-negative integral solutions of equation x1 + x2 +…+ xn = k is equal to the number of ways in which k identical balls can be distributed into N unique boxes.
- Number of positive integral solutions of equation x1 + x2 + … + xn = k is equal to the number of ways in which k identical balls can be distributed into N unique boxes such that each box must contain at-least 1 ball.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...