VOOZH about

URL: https://www.geeksforgeeks.org/dsa/stars-and-bars-algorithms-for-competitive-programming/

⇱ Stars and Bars Algorithms for Competitive Programming - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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:

Where:

  • C(n, r) represents the binomial coefficient
  • It is defined as:

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: , where C(n, r) denotes the binomial coefficient.

Example:

👁 1

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: , where C(n, r) denotes the binomial coefficient.

Example:

👁 2

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