Can You Solve a High Schooler's Favorite Puzzle?
The Problem
A teacher is handing out candy to his students, of which there are at least four. He abides by the following rules:
He hands out candy to groups of three students (i.e., "trios") at a time. Each member of the trio gets one piece of candy.
Each unique trio can ask for candy, but that same trio can't come back for seconds. If students in the trio want more candy, they must return as part of a different trio.
When a trio gets candy, the next trio can't contain any students from that previous trio.
It turns out that every possible trio can get a helping of candy. What is the smallest class size for which this is possible?
My Approach to Finding the Answer
Beneath the scenes this is a graph-based problem. Each node represents a possible trio of students, and two nodes are connected by an edge if they don't share any students in common (meaning they can follow each other in the sequence).
By modeling it as such and using a greedy approach for finding a valid traversal that hits all nodes, we are able to find a path that includes all 35 combinations for the 7 student case.

Click to expand the complete sequence of 35 trios
- (0, 1, 5)
- (2, 3, 6)
- (1, 4, 5)
- (0, 3, 6)
- (2, 4, 5)
- (0, 1, 3)
- (2, 4, 6)
- (1, 3, 5)
- (0, 2, 4)
- (1, 5, 6)
- (0, 3, 4)
- (2, 5, 6)
- (0, 1, 4)
- (2, 3, 5)
- (0, 1, 6)
- (2, 3, 4)
- (0, 5, 6)
- (1, 3, 4)
- (0, 2, 6)
- (3, 4, 5)
- (1, 2, 6)
- (0, 3, 5)
- (1, 2, 4)
- (3, 5, 6)
- (0, 1, 2)
- (3, 4, 6)
- (1, 2, 5)
- (0, 4, 6)
- (1, 2, 3)
- (0, 4, 5)
- (1, 3, 6)
- (0, 2, 5)
- (1, 4, 6)
- (0, 2, 3)
- (4, 5, 6)
This complete sequence demonstrates that all 35 possible combinations of 3 students from a class of 7 can receive candy while following all the given constraints.
Proving This is the Minimum
We can confirm that 7 is the smallest possible class size, since the case with 6 students is impossible. With 6 students, there are 20 possible trios, but each trio can only be followed by exactly one other trio (the trio containing the remaining 3 students), creating a severe constraint that makes a complete sequence impossible.
The graph analysis confirms this limitation - with 6 students, the compatibility graph has a very limited structure that cannot support a path visiting all nodes.

Computational Challenges
The problem was not feasible to brute force due to its complexity. The total number of possible valid orderings is:
For a class size () of 7, this equates to approximately (10 duodecillion) possible orderings. Even with various optimizations, exhaustively searching this space is computationally infeasible.
To overcome this challenge, we used a greedy approach with a heuristic based on the number of valid remaining options. This approach required only hundreds of search attempts to find a complete solution that includes all possible trios.
Conclusion
This puzzle was a bit more straightforward than I expected but still proved to be a fun solve