Number of integral solutions of the equation x1 + x2 +…. + xN = k

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

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)!.

Below is the implementation of above approach:

c++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for above implementation 
#include<iostream>
  
using namespace std ;
  
int nCr(int n, int r)
{
    int fac[100] = {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 ;
}
  
// Driver Code
int main()
{
    int n = 3 ;
    int k = 3 ;
      
    int ans = nCr(n + k - 1 , k) + 
              nCr(k - 1, n - 1);
      
    cout << ans ;
      
    return 0 ; 
}
  
// This code is contributed
// by ANKITRAI1

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for above implementation
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 ;
}
  
// Driver Code
public static void main (String[] args) 
{
    int n = 3 ;
    int k = 3 ;
      
    int ans = nCr(n + k - 1 , k) + 
              nCr(k - 1, n - 1);
      
    System.out.println(ans) ;
}
}
  
// This code is contributed
// by anuj_67

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python implementation of 
# above approach
  
# Calculate nCr i.e binomial 
# cofficent nCr = n !/(r !*(n-r)!)
def nCr(n, r):
  
    # first find factorial 
    # upto n
    fac = list()
    fac.append(1)
    for i in range(1, n + 1):
        fac.append(fac[i - 1] * i)
  
    # use nCr formula
    ans = fac[n] / (fac[n - r] * fac[r])
    return ans
  
# n = number of variables
n = 3
  
# sum of n variables = k
k = 3
  
# find number of solutions
ans = nCr(n + k - 1, k) + nCr(k - 1, n - 1)
  
print(ans)
  
# This code is contributed
# by ChitraNayal

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for above implementation
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 ;
}
  
// Driver Code
public static void Main () 
{
    int n = 3 ;
    int k = 3 ;
      
    int ans = nCr(n + k - 1 , k) + 
              nCr(k - 1, n - 1);
      
    Console.Write(ans) ;
}
}
  
// This code is contributed 
// by ChitraNayal

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation of above approach
  
// Calculate nCr i.e binomial 
// cofficent nCr = n !/(r !*(n-r)!)
function nCr($n, $r)
{
    // first find factorial 
    // upto n
    $fac = array();
    array_push($fac, 1);
    for($i = 1; $i < $n + 1; $i++)
        array_push($fac, $fac[$i - 1] * $i);
  
    // use nCr formula
    $ans = $fac[$n] / ($fac[$n - $r] * 
                       $fac[$r]);
    return $ans;
}
  
// Driver Code
  
// n = number of variables
$n = 3;
  
// sum of n variables = k
$k = 3;
  
// find number of solutions
$ans = nCr($n + $k - 1, $k) +
       nCr($k - 1, $n - 1);
  
print($ans);
  
// This code is contributed
// by mits
?>

chevron_right


Output:

11.0

Applications of the above concepts:

  1. 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.
  2. 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.


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.