Stars and Bars Algorithms for Competitive Programming

Last Updated : 28 May, 2026

The Stars and Bars (also known as Balls and Urns) technique is a widely used method in Combinatorics, which is the branch of mathematics dealing with counting and arrangements.

  • It provides a simple and intuitive way to solve problems related to distributing identical objects into distinct groups.
  • This method is especially useful in problems where we need to count the number of ways to distribute items under certain constraints.

The core idea of the Stars and Bars method is both simple and powerful.

  • Represent n identical objects as stars (*)
  • Represent k groups (or partitions) as bars (|)

To divide the objects into groups, We choose positions of (k − 1) bars among (n + k − 1) total positions.

Formula: The number of ways to distribute n identical objects into k distinct groups (where groups can be empty) is given by: C(n+k−1, k−1)

Where:

  • C(n, r) represents the binomial coefficient
  • It is defined as: C(n, r) = \frac{n!}{r!(n - r)!}

We need to place (k − 1) bars among n stars to divide them into k groups. Thus, the problem reduces to choosing positions for the bars from a total of (n + k − 1) positions.

Variations of Stars and Bars

The Stars and Bars technique has two important variations depending on constraints:

1. Distribution among groups where groups can have 0 objects

Consider that we have n identical objects (represented as stars) and we want to distribute them into k distinct groups. Each group is allowed to have zero or more objects. According to the Stars and Bars theorem, the number of ways to perform this distribution is: C(n+k−1,k−1), where C(n, r) denotes the binomial coefficient.

Example:

1
C++
#include <bits/stdc++.h>
using namespace std;

// Function to calculate nCr (binomial coefficient)
int count(int n, int r)
{
    r = min(r, n - r);
    int ans = 1;

    for (int i = 0; i < r; i++)
    {
        ans = ans * (n - i);
        ans = ans / (i + 1);
    }

    return ans;
}

// Function to get number of ways (groups can have 0 objects)
int getWaysWithZero(int n, int k)
{
    return count(n + k - 1, k - 1);
}

// Driver code
int main()
{
    int n = 4, k = 2;

    cout << getWaysWithZero(n, k);

    return 0;
}
Java
import java.util.Arrays;

// Function to calculate nCr (binomial coefficient)
public class GfG {
    public static int count(int n, int r) {
        r = Math.min(r, n - r);
        int ans = 1;
        for (int i = 0; i < r; i++) {
            ans = ans * (n - i);
            ans = ans / (i + 1);
        }
        return ans;
    }

    // Function to get number of ways (groups can have 0 objects)
    public static int getWaysWithZero(int n, int k) {
        return count(n + k - 1, k - 1);
    }

    // Driver code
    public static void main(String[] args) {
        int n = 4, k = 2;
        System.out.println(getWaysWithZero(n, k));
    }
}
Python
from math import comb

# Function to calculate nCr (binomial coefficient)
def count(n, r):
    r = min(r, n - r)
    ans = 1
    for i in range(r):
        ans = ans * (n - i)
        ans = ans // (i + 1)
    return ans

# Function to get number of ways (groups can have 0 objects)
def getWaysWithZero(n, k):
    return count(n + k - 1, k - 1)

# Driver code
if __name__ == "__main__":
    n = 4
    k = 2
    print(getWaysWithZero(n, k))
C#
using System;

// Function to calculate nCr (binomial coefficient)
public class GfG {
    public static int count(int n, int r) {
        r = Math.Min(r, n - r);
        int ans = 1;
        for (int i = 0; i < r; i++) {
            ans = ans * (n - i);
            ans = ans / (i + 1);
        }
        return ans;
    }

    // Function to get number of ways (groups can have 0 objects)
    public static int getWaysWithZero(int n, int k) {
        return count(n + k - 1, k - 1);
    }

    // Driver code
    public static void Main() {
        int n = 4, k = 2;
        Console.WriteLine(getWaysWithZero(n, k));
    }
}
JavaScript
// Function to calculate nCr (binomial coefficient)
function count(n, r) {
    r = Math.min(r, n - r);
    let ans = 1;
    for (let i = 0; i < r; i++) {
        ans = ans * (n - i);
        ans = Math.floor(ans / (i + 1));
    }
    return ans;
}

// Function to get number of ways (groups can have 0 objects)
function getWaysWithZero(n, k) {
    return count(n + k - 1, k - 1);
}

// Driver code
let n = 4, k = 2;
console.log(getWaysWithZero(n, k));

Output
5

Time Complexity: O(k), where k is the number of groups among which objects are distributed.
Auxiliary Space: O(1)

2. Distribution among groups where groups cannot have 0 objects

Consider that we have n identical objects (represented as stars) and we want to distribute them into k distinct groups such that each group must contain at least one object. According to the Stars and Bars theorem, the number of ways to distribute the objects is: C(n−1,k−1), where C(n, r) denotes the binomial coefficient.

Example:

2
C++
#include <bits/stdc++.h>
using namespace std;

// Function to calculate nCr (binomial coefficient)
// using an efficient iterative approach
int count(int n, int r)
{
    // Since C(n, r) = C(n, n-r), take minimum
    r = min(r, n - r);

    int ans = 1;

    // Compute result iteratively
    for (int i = 0; i < r; i++) {
        ans = ans * (n - i);
        ans = ans / (i + 1);
    }

    return ans;
}

// Function to return number of ways to distribute
// n identical objects into k groups such that
// no group is empty
int getWaysWithoutZero(int n, int k)
{
    // Apply Stars and Bars formula: C(n-1, k-1)
    return count(n - 1, k - 1);
}

// Driver code
int main()
{

    int n = 4, k = 2;

    cout << getWaysWithoutZero(n, k);

    return 0;
}
Java
import java.util.Scanner;

// Function to calculate nCr (binomial coefficient)
// using an efficient iterative approach
public class GfG {
    public static int count(int n, int r) {
        // Since C(n, r) = C(n, n-r), take minimum
        r = Math.min(r, n - r);

        int ans = 1;

        // Compute result iteratively
        for (int i = 0; i < r; i++) {
            ans = ans * (n - i);
            ans = ans / (i + 1);
        }

        return ans;
    }

    // Function to return number of ways to distribute
    // n identical objects into k groups such that
    // no group is empty
    public static int getWaysWithoutZero(int n, int k) {
        // Apply Stars and Bars formula: C(n-1, k-1)
        return count(n - 1, k - 1);
    }

    // Driver code
    public static void main(String[] args) {
        int n = 4, k = 2;

        System.out.println(getWaysWithoutZero(n, k));
    }
}
Python
from math import comb

# Function to calculate nCr (binomial coefficient)
def count(n, r):
    r = min(r, n - r)
    ans = 1

    for i in range(r):
        ans = ans * (n - i)
        ans = ans // (i + 1)

    return ans

# Function for distribution where no group is empty
def getWaysWithoutZero(n, k):
    return count(n - 1, k - 1)

# Driver code
if __name__ == "__main__":
    n = 4
    k = 2
    print(getWaysWithoutZero(n, k))
C#
using System;

// Function to calculate nCr (binomial coefficient)
// using an efficient iterative approach
public class GfG {
    public static int Count(int n, int r) {
        // Since C(n, r) = C(n, n-r), take minimum
        r = Math.Min(r, n - r);

        int ans = 1;

        // Compute result iteratively
        for (int i = 0; i < r; i++) {
            ans = ans * (n - i);
            ans = ans / (i + 1);
        }

        return ans;
    }

    // Function to return number of ways to distribute
    // n identical objects into k groups such that
    // no group is empty
    public static int GetWaysWithoutZero(int n, int k) {
        // Apply Stars and Bars formula: C(n-1, k-1)
        return Count(n - 1, k - 1);
    }

    // Driver code
    public static void Main() {
        int n = 4, k = 2;

        Console.WriteLine(GetWaysWithoutZero(n, k));
    }
}
JavaScript
// Function to calculate nCr (binomial coefficient)
// using an efficient iterative approach
function count(n, r) {
    // Since C(n, r) = C(n, n-r), take minimum
    r = Math.min(r, n - r);

    let ans = 1;

    // Compute result iteratively
    for (let i = 0; i < r; i++) {
        ans = ans * (n - i);
        ans = Math.floor(ans / (i + 1));
    }

    return ans;
}

// Function to return number of ways to distribute
// n identical objects into k groups such that
// no group is empty
function getWaysWithoutZero(n, k) {
    // Apply Stars and Bars formula: C(n-1, k-1)
    return count(n - 1, k - 1);
}

// Driver code
const n = 4, k = 2;

console.log(getWaysWithoutZero(n, k));

Output
3

Time Complexity: O(k), where k is the number of groups among which objects are distributed.
Auxiliary Space: O(1)

Real Life Applications of Stars and Bars:

  • Combinatorial Design: In combinatorial design, the Stars and Bars approach can be used to construct designs with certain properties. For example, it can be used to determine the number of ways to design an experiment or survey.
  • Computer Science: In computer science, the Stars and Bars approach can be used in algorithm design and analysis. For example, it can be used to count the number of ways to allocate resources or to distribute tasks among processors.
  • Coding Theory: In coding theory, the Stars and Bars approach can be used to count the number of codewords of a certain weight in a block code.

Related Article:

Comment