The Great Elephant Race

2025-08-31

Blog Cover

Background

While at work last week, my coworker gave me an interesting riddle. After working through it myself and giving it to some friends, I decided to make a little write up on the riddle and its solutions.

The Riddle

There are 900 elephants in a race. Bob and Charlie know which elephants are in 1st and 2nd place.

You are allowed to ask Bob a question about the race that he will respond to with a number from 1 to nn. After that Charlie will give you the number of either the 1st or 2nd place elephant.

You have to use Bob's answer to determine whether Charlie's number is the 1st or 2nd place elephant.

What is the lowest upper bound of nn Bob can give you so that you can determine if the elephant Charlie gave you finished in 1st or 2nd?

Clarifications

Can you ask Bob which elephant got first place?

Yes, if you ask Bob who got first place, you can use his answer to determine whether the elephant Charlie gives placed 1st or 2nd. However the upper bound for nn in this case is 900 (there are better answers).

Can Bob answer with fractions or decimals?

No, he must either answer with whole numbers or have the ability to map his possible answers to whole numbers.

Can you ask Bob multiple questions?

You can make the question you ask Bob as long as you want. However, Bob is only allowed to answer with a single number. You are allowed to make each digit depend on a sub-question, but then the lowest upper bound would be the range of possibilities that Bob would answer with.

For example, you could ask Bob 2 yes or no questions and have him answer with a 2 digit number (1 digit per question) where 1 means yes and 2 means no. Since there would be 4 possible answers Bob could give (11,12,21,22), the upper bound for nn would be 4.

This Sounds Like a Math Problem?

What problem isn't a math problem! Here's a version of the riddle formatted as a math question:

  • Let AA = The set of answers Bob can give
  • Let EE = The set of unique elephants in the race
  • Let F={f:E×E×A{0,1}}\mathcal{F} = \{f : E \times E \times A \to \{0,1\}\} be the set of questions you can ask Bob where:
    • f(🐘a,🐘b,ans)=1f(\text{🐘}_a, \text{🐘}_b, \text{ans}) = 1 if elephants 🐘a\text{🐘}_a and 🐘b\text{🐘}_b can be distinguished using Bob's answer ans\text{ans}
    • f(🐘a,🐘b,ans)=0f(\text{🐘}_a, \text{🐘}_b, \text{ans}) = 0 otherwise

Find the minimum possible A|A| such that:

🐘a,🐘bE,ansA,fF:(🐘a=🐘b)    (f(🐘a,🐘b,ans)=1)\begin{align*} &\forall \text{🐘}_a, \text{🐘}_b \in E, \\ &\exists \text{ans} \in A, \\ &\exists f \in \mathcal{F} : \\ &(\text{🐘}_a = \text{🐘}_b) \implies (f(\text{🐘}_a, \text{🐘}_b, \text{ans}) = 1) \end{align*}

Can Bob tell the future?

A peculiar individual asked me this question to which I answered yes out of curiosity. He then answered: "I would ask Bob to say 1 if Charlie is going to say the 1st place elephant, and 2 if Charlie says the 2nd place elephant". I have not seen a solution with a lower bound than this.

The Intuitive Solution

A non-optimal but still efficient way of solving this problem is as follows:

  1. Have Bob tell you a position of the first digit in the 1st place elephant that is different in the 2nd place elephant.
    • He will say 1 if it's the ones digit, 2 if it's the tens digit, and 3 if it's the hundreds digit.
  2. Have Bob tell you what the value of that digit is for the 1st place elephant.
    • He will answer this as a number from 0-9.
  3. Have Bob combine the 2 numbers as his answer

Since there are 30 possible answers Bob can give (3 max digits ×\times 10 possible values), the lowest upper bound that Bob can give you is n=30n = 30.

Example of the Solution

  • Let the 1st place elephant be 🐘45\text{🐘}_{45} and the 2nd place elephant be 🐘149\text{🐘}_{149}
  • Bob will answer "15" since the ones digit is is "5" in the 1st place elephant and "9" in the 2nd place elephant
  • There are 2 numbers that Charlie can give you:
    • If Charlie gives you the 1st place elephant (🐘45\text{🐘}_{45}), you can see that the ones place digit is "5". Since you know the 1st place elephant has this characteristic and the 2nd place elephant doesn't, this must be the 1st place elephant.
    • If Charlie gives you the 2nd place elephant (🐘149\text{🐘}_{149}), you can see that the ones place digit is "9". Since you know the 1st place has a "5" in the ones place, this must be the 2nd place elephant.

Optimizations

The intuitive solution assumes a base 10 number system. Surprisingly, different number systems like Base 12 and Base 7 will give different answers. Below is a more general approach that accounts for different bases:

  1. Let BB be the base of the chosen number system (decimal, binary, etc.)
  2. Determine the maximum number of digits needed to represent 900
    • We'll refer to this as DD
    • D=logB(900)D = \lceil \log_B(900) \rceil
  3. Have Bob return position of digit along with value
    • Bob will answer in format (d,v)(d, v) where d[1,D]d \in [1, D] and v[0,B1]v \in [0, B-1]
      • dd represents the digit position (1st, 2nd, 3rd, etc.)
      • vv represents the digit value in base BB

In this solution, the minimum upper bound of answers will be n=D×Bn = D \times B. However, since we know that D=logB(900)D = \lceil \log_B(900) \rceil, we can simplify the upper bound to n=logB(900)×Bn = \lceil \log_B(900) \rceil \times B. Below are some example bases and their respective results:

BB (Base) logB(900)\lceil \log_B(900) \rceil (Digits Needed) D×BD \times B (Min Upper Bound)
2 10 20
3 7 21
5 4 20
7 4 28
9 4 36
10 3 30
12 3 36
30 2 60

What's Going on Here?

Why does changing the base of the number system change the minimum upper bound so much? To answer this, let's dive deeper into what's happening in our solution. When we choose a base for our strategy, we're implicitly creating D×BD \times B different groupings of elephants. Each group is defined by fixing a value of a specific digit to a constant value.

Here are the groupings for B=10B=10 with padded zeros to more clearly show the pattern:

Group Bob's Label Elephants in Group
G1G_{1} (1,0) 🐘000\text{🐘}_{000}1, 🐘010\text{🐘}_{010}, 🐘020\text{🐘}_{020}, ..., 🐘970\text{🐘}_{970}, 🐘980\text{🐘}_{980}, 🐘990\text{🐘}_{990}
G2G_{2} (1,1) 🐘001\text{🐘}_{001}, 🐘011\text{🐘}_{011}, 🐘021\text{🐘}_{021}, ..., 🐘971\text{🐘}_{971}, 🐘981\text{🐘}_{981}, 🐘991\text{🐘}_{991}
G3G_{3} (1,2) 🐘002\text{🐘}_{002}, 🐘012\text{🐘}_{012}, 🐘022\text{🐘}_{022}, ..., 🐘972\text{🐘}_{972}, 🐘982\text{🐘}_{982}, 🐘992\text{🐘}_{992}
... ... ...
G28G_{28} (3,7) 🐘700\text{🐘}_{700}, 🐘701\text{🐘}_{701}, 🐘702\text{🐘}_{702}, ..., 🐘797\text{🐘}_{797}, 🐘798\text{🐘}_{798}, 🐘799\text{🐘}_{799}
G29G_{29} (3,8) 🐘800\text{🐘}_{800}, 🐘801\text{🐘}_{801}, 🐘802\text{🐘}_{802}, ..., 🐘897\text{🐘}_{897}, 🐘898\text{🐘}_{898}, 🐘899\text{🐘}_{899}
G30G_{30} (3,9) 🐘900\text{🐘}_{900}, 🐘901\text{🐘}_{901}, 🐘902\text{🐘}_{902}, ..., 🐘997\text{🐘}_{997}, 🐘998\text{🐘}_{998}, 🐘999\text{🐘}_{999}1

Since there are 2 digits unconstrained in each group, there are 102=10010^2=100 elements per group1. This means the total number of elephants including duplicates is 30×100=300030 \times 100 = 3000, meaning each elephant appears in exactly 3 groups 1.

Using the Groups

Now let's explore why this way of grouping elephants helps Bob answer our question. When we ask Bob to tell us which digit is different and the value it should be, we're essentially asking Bob to tell us which of the above groups contains the 1st place elephant but not the 2nd place elephant. Once Bob gives us a group, we just check if the elephant Charlie gives us is in that group. If it's in the group, Charlie gave us the 1st place elephant. Otherwise we know it's the 2nd place elephant.

We know that there is guaranteed to be a group that contains 1st place but not 2nd place because of two reasons:

  1. The 1st and 2nd place elephants are represented by two different numbers
  2. Every pair of 2 distinct numbers must have at least 1 different digit for all number systems

The Advanced Answer

With the previous strategy, we discovered that we are actually turning the 900 elephants into 30 groups (for B=10B=10) such that for distinct values 🐘a\text{🐘}_a and 🐘b\text{🐘}_b (1st and 2nd place elephants), there is guaranteed to be a group that contains 🐘a\text{🐘}_a but not 🐘b\text{🐘}_b.

Now we wish to determine the minimum number of groups we need to be able to distinguish the 1st from 2nd elephant given 900 distinct elephants.

Grouping by Hand

Let's start by hand choosing groups for smaller numbers of elephants:

Num Elephants = 3 Num Elephants = 4 Num Elephants = 5
G1={🐘1,🐘2}G_1 = \{\text{🐘}_1, \text{🐘}_2\} G1={🐘2,🐘3}G_1 = \{\text{🐘}_2, \text{🐘}_3\} G1={🐘1,🐘2,🐘3}G_1 = \{\text{🐘}_1, \text{🐘}_2, \text{🐘}_3\}
G2={🐘1,🐘3}G_2 = \{\text{🐘}_1, \text{🐘}_3\} G2={🐘1,🐘4}G_2 = \{\text{🐘}_1, \text{🐘}_4\} G2={🐘1,🐘4,🐘5}G_2 = \{\text{🐘}_1, \text{🐘}_4, \text{🐘}_5\}
G3={🐘2,🐘3}G_3 = \{\text{🐘}_2, \text{🐘}_3\} G3={🐘2,🐘4}G_3 = \{\text{🐘}_2, \text{🐘}_4\} G3={🐘2,🐘4}G_3 = \{\text{🐘}_2, \text{🐘}_4\}
G4={🐘1,🐘3}G_4 = \{\text{🐘}_1, \text{🐘}_3\} G4={🐘3,🐘5}G_4 = \{\text{🐘}_3, \text{🐘}_5\}

These groupings work because all elephants appear in at least 2 groups which have no shared items besides the elephant in question. This means then for all possible 1st and 2nd place pairs that Charlie can give, Bob can give the number of the group containing only the 1st place elephant.

A Better Representation

Now let's try to generalize this to larger groups of elephants. Instead of explicitly showing the groups, let's assume nn groups and give each elephant an nn length encoding that says what groups they are a part of. For position ii of the encoding, let 1 signal that the elephant is part of group ii and 0 that the elephant is not in group ii.

G cluster_0 cluster_1 A Num Elephants = 5 Num Groups = 4 G₁ = {🐘₁, 🐘₂, 🐘₃} G₂ = {🐘₁, 🐘₄, 🐘₅} G₃ = {🐘₂, 🐘₄} G₄ = {🐘₃, 🐘₅} B Binary Encodings 🐘₁ → 1100 🐘₂ → 1010 🐘₃ → 1001 🐘₄ → 0110 🐘₅ → 0101 A->B

Using the Representation

To determine if any 1st and 2nd place elephant pair Charlie can give is valid, we can now just compare each elephant's encoding to see if there is a position where 1st place has a 1 and 2nd place has a zero.

Below is an example of how you could use the above encoding to determine:

  • 1st Place = 🐘3\text{🐘}_3 and 2nd Place = 🐘5\text{🐘}_5
  • Encodings:
    • 🐘3=1001\text{🐘}_3 = 1001
    • 🐘5=0101\text{🐘}_5 = 0101
  • Since Position 1 for 1st place elephant is 1 while 0 for 2nd place elephant, we know Group 1 contains 1st place elephant and not 2nd place elephant. Therefore Bob will answer Group 1.
  • There are 2 numbers that Charlie can give you:
    • If Charlie gives you the 1st place elephant (🐘3\text{🐘}_3), we see from 🐘3\text{🐘}_3's encoding (1001) that it is in Group 1 and therefore must be the 1st place elephant.
    • If Charlie gives you the 2nd place elephant (🐘5\text{🐘}_5), we see from 🐘5\text{🐘}_5's encoding (0101) that it isn't in Group 1 and therefore must be the 2nd place elephant.

The Underlying Pattern

If you look closely you'll also notice that each elephant encoding has exactly 2 1s in its encoding of length 4. An interesting property of this representation is that as long as every elephant is in exactly kk groups (when a 1 appears kk times in the elephants encoding), then for any 2 elephants 🐘a\text{🐘}_{a} and 🐘b\text{🐘}_{b}, there will be a position in the encoding such that elephant 🐘a\text{🐘}_{a} will have value 1 while elephant 🐘b\text{🐘}_{b} has value 0. In the original solution, each elephant appeared in exactly 3 different groups out of the 30 total showing it also follows this pattern.

What are the rules for picking the number of groups (nn) and number of 1s (kk) allowed for each encoding? With nn groups and a fixed kk, we can derive the number of unique encodings with (nk)\binom{n}{k}. So for our example above when n=4n=4 and k=2k=2, we can uniquely represent a max of (42)=6\binom{4}{2}=6 elephants.

We now need to find the optimal kk such that we can minimize nn for 900 elephants. To do this, we need to find the minimum nn such that k:(nk)900\exists k: \binom{n}{k} \geq 900. Before we try to solve that directly2, let's try and simplify the problem a little more.

Optimizing the Pattern

You may notice when you plug in (nk)\binom{n}{k} with different nn and kk values, it tends to be maximal for a given nn when we choose a kk that is about n/2n/2. This can be formalized with the below theorem:

Sperner's Theorem: The largest possible family of non-overlapping subsets for an nn sized set can be calculated as:

S(nn/2)|S| \leq \binom{n}{\left\lfloor n/2 \right\rfloor}

If and only if each subset has size k=n2k = \left\lfloor \frac{n}{2} \right\rfloor or k=n2k = \left\lceil \frac{n}{2} \right\rceil

We can now use this theorem to more simply represent the problem. Let's start by testing values nn with the equation 900(nn/2)900 \leq \binom{n}{\left\lfloor n/2 \right\rfloor} on different values until we find the minimum number of groups that work.

If we try with n=11n=11, we get (1111/2)=(115)=462\binom{11}{\left\lfloor 11/2 \right\rfloor} = \binom{11}{5} = 462. This shows us we need more than 11 groups as 462<900462 < 900.

If we try with n=12n=12, we get (1212/2)=(126)=924\binom{12}{\left\lfloor 12/2 \right\rfloor} = \binom{12}{6} = 924. As 924>900924 > 900 we know this is a valid number of groupings. Additionally, since we know n=11n=11 doesn't work and we need to use whole numbers for numbers of groups, the minimum upper bound Bob can give is n=12n=12 3.

We can generalize this to different sized elephant races by using a binary search from 2 to the size of the race to find the minimum number of groups using the above equation. I created a handy little visualization tool to help create visualizations for generating the optimal groups below:

Visualization Tool

Conclusion

In this post, we first determined a way to distinguish any 2 elephants of a 900 elephant race from a set of 30 possible answers based on the digits of the elephants' numbers. We then discovered we can lower the set of possible answers to 20 by using a different number system such as binary instead of decimal. After this we dug deeper into why this trick worked and used the intuition to calculate the smallest possible set of answers3 which turned out to be 12.

The next time you're at an elephant race and have an annoying friend named Charlie who loves giving cryptic answers, make sure you use this strategy when you talk to Bob!

Footnotes

  1. We're including 🐘000\text{🐘}_{000} and any elephant above 900 in these calculations despite them not being in the race. This is because you can use this strategy for races of up to 1000 elephants. ↩︎ ↩︎ ↩︎ ↩︎

  2. You can plug this in to Desmos to solve ↩︎

  3. This is the minimum bound with this method, there may or may not be an even lower bound ↩︎ ↩︎