Search Results

Documents authored by Stevens, Brett


Document
Pyramid Schemes for Eating M&Ms: Enumeration, Generation, and Gray Codes

Authors: Elizabeth Hartung, Brett Stevens, and Aaron Williams

Published in: LIPIcs, Volume 366, 13th International Conference on Fun with Algorithms (FUN 2026)


Abstract
Consider the following problem. You have a rainbow pyramid of M&Ms with n rows. For example, when n = 4 you may have one red, two orange, three yellow, and four green {inline.pdf}. You want to eat n of the M&Ms in such a way that the remaining M&Ms can be rearranged into a rainbow pyramid with n-1 rows. Two approaches are distinct if a different number from a particular row are eaten. In other words, we only care about the multiset of row frequencies (or colours) that are eaten and not the order in which they are eaten. One solution eats one M&M per row (e.g., 1234 → {inline.pdf}). Another eats the entire bottom row (e.g., 4444 → {inline.pdf}). How many different solutions are there? We show that the answer is 2^{n-1}. Furthermore, each solution can be naturally encoded with combinatorial objects enumerated by 2^{n-1} including binary words of length n-1, compositions of n, and subsets of [n-1]. Less obviously they are encoded by M&M permutations where each value in [n] is at most one position to the right of its position in the identity (e.g., 123, 132, 213, 312 for n = 3). What if at most m from each row can be eaten? When m = 1 the only solution is to eat one of each colour. Otherwise, the solutions are counted by Fibonacci (m = 2), Tribonacci (m = 3), Tetranacci (m = 4), and so on, up to 2^{n-1} (m = n). Furthermore, solutions can be naturally encoded by limited versions of the aforementioned objects including binary strings avoiding the substring 0^{m} and M&M permutations where values are limited by moving at most 𝓁 = m-1 positions to the left. Motivated by the works of Samuel Beckett, we consider minimal-change orders of the solutions. We obtain a satisfying result by filtering the binary reflected Gray code to words avoiding 0^{m}. For example, when n = 4 we have BRGC(n) = 000, 100, 110, 010, 011, 111, 101, 001 and the words avoiding 00 are BRGC_𝓁(3) = 110, 010, 011, 111, 101 where 𝓁 = 1 is the limit on the run-lengths of 0s. Our bijection then creates solutions that differ in by a single M&M 1244, 2244, 2234,1234, 1334. Thus, Beckett’s character Murphy can imagine every experience by changing one M&M at a time. The generalized Gray code BRGC_𝓁(n) was previously defined recursively [Bernini et. al Acta Informatica 2015] with its change sequence supporting amortized 𝒪(1)-time generation [Arndt Matters Computational 2010]. We uncover a simple greedy definition - flip the leftmost bit that creates a new binary word avoiding 0^m starting from w = ⋯ 110^{𝓁}110^{𝓁} - and a successor rule that supports loopless worst-case 𝒪(1)-time generation. Furthermore, the corresponding limited M&M permutations are greedily generated by swapping the smallest value (or the leftmost pair of adjacent values) that gives a valid new permutation (e.g., ̅{12}43, 21 ̅{43}, ̅{21}34, 1 ̅{23}4, 1324 for n = 4 and 𝓁 = 1). We also consider a relaxed version of the problem in which the initial pyramid’s n rows have respective widths r, r+1, r+2, …, n, n, …, n. Here the answer is an n-term product ⟨n,r⟩! = 1 ⋅ 2 ⋅ 3 ⋯ r ⋅ (r+1) ⋅ (r+1) ⋯ (r+1) that we refer to as a flatorial number. Furthermore, the solutions are represented by a generalization of M&M permutations in which each symbol can appear at most r positions to the right of its position in the identity. We complete our investigation by showing that eight distinct classes of permutations are enumerated by flatorial numbers.

Cite as

Elizabeth Hartung, Brett Stevens, and Aaron Williams. Pyramid Schemes for Eating M&Ms: Enumeration, Generation, and Gray Codes. In 13th International Conference on Fun with Algorithms (FUN 2026). Leibniz International Proceedings in Informatics (LIPIcs), Volume 366, pp. 23:1-23:24, Schloss Dagstuhl – Leibniz-Zentrum für Informatik (2026)


Copy BibTex To Clipboard

@InProceedings{hartung_et_al:LIPIcs.FUN.2026.23,
  author =	{Hartung, Elizabeth and Stevens, Brett and Williams, Aaron},
  title =	{{Pyramid Schemes for Eating M\&Ms: Enumeration, Generation, and Gray Codes}},
  booktitle =	{13th International Conference on Fun with Algorithms (FUN 2026)},
  pages =	{23:1--23:24},
  series =	{Leibniz International Proceedings in Informatics (LIPIcs)},
  ISBN =	{978-3-95977-417-8},
  ISSN =	{1868-8969},
  year =	{2026},
  volume =	{366},
  editor =	{Iacono, John},
  publisher =	{Schloss Dagstuhl -- Leibniz-Zentrum f{\"u}r Informatik},
  address =	{Dagstuhl, Germany},
  URL =		{https://drops.dagstuhl.de/entities/document/10.4230/LIPIcs.FUN.2026.23},
  URN =		{urn:nbn:de:0030-drops-257420},
  doi =		{10.4230/LIPIcs.FUN.2026.23},
  annote =	{Keywords: combinatorial enumeration, generation, Gray code, loopless algorithm}
}
Any Issues?
X

Feedback on the Current Page

CAPTCHA

Thanks for your feedback!

Feedback submitted to Dagstuhl Publishing

Could not send message

Please try again later or send an E-mail