This Riddler problem is a logic puzzle about liars and truth-tellers.

You are the emperor of Byzantium (lucky you!) and you have N generals working for you. You know that more than half of your generals are loyal, and the rest are traitors. You can ask any general about the loyalty of any other general: If the general you ask is loyal, he will answer correctly, but if he is a traitor he can answer however he likes. Your goal is to find one general you are absolutely certain is loyal while asking the fewest possible questions.

What is the minimum number of questions (in terms of N) that will guarantee a solution, and what strategy produces it?

There is a problem in cryptography known as the Byzantine Generals Problem, which has to do with achieving consensus in the presence of traitors that can sabotage communications. The Riddler problem above also involves liars and truth-tellers, but it’s a very different problem.

The following is adapted from a comment by Guy Moore. A similar solution that obtains the same final result was also found by Dmytro Taranovsky. Thank you both for your insights!

[Show Solution]

The $N$ generals have the property that there is a loyal majority. This solution efficiently eliminates generals such that the remaining group of generals preserves the loyalty majority property. The strategy is:

- If there is an even number of generals, eliminate one of the generals (doesn’t matter which).
- If there is an odd number of generals, set one general aside. Pair up the rest of the generals and for each pair, ask the first general if the second one is a traitor.
- If the first general says the other is a traitor, eliminate both generals.
- If the first general says the other is loyal, eliminate the first general and keep the second.

- If we kept an odd number of generals, eliminate the general that was set aside. Otherwise, re-include the general that was set aside.
- If there is only one general left, this general is loyal and we may stop. Otherwise, return to the first step and repeat the process with the generals that are left.

### Why does this work?

At each step, the loyalty majority property is preserved. To see why, let’s examine each case.

- If the first general says the second is a traitor, they cannot both be loyal. Therefore, by eliminating both, we eliminate at least as many traitors as loyal generals.
- If the first general says the second is loyal, we eliminate the first one. If the general that remains is a traitor, then both of them were traitors. This can happen for at most half of the pairs, since otherwise there would be a majority of traitors. If it happens for exactly half of the pairs, then the general we set aside must be loyal and so by re-including the general we set aside we retain loyal majority.
- If there is only one general remaining, then the loyalty majority implies that this general must be loyal.

### How well does the strategy perform?

If we have $k$ generals, we ask $\left\lfloor{\frac{k-1}{2}}\right\rfloor$ questions. In the worst case, each question leads to only one elimination. We also re-include the general we set aside depending on parity. So the number of generals remaining in the next round is:

\[

\begin{cases}

\left\lfloor\frac{k-1}{2}\right\rfloor & \text{if }k\text{ is odd}\\

\left\lfloor\frac{k-1}{2}\right\rfloor + 1 & \text{if }k\text{ is even}

\end{cases}

\]

This is equivalent to

\[

1 + 2\left\lfloor\frac{1}{2}\left\lfloor\frac{k-1}{2}\right\rfloor\right\rfloor = 1 + 2\left\lfloor\frac{k-1}{4}\right\rfloor

\]

We can make this simplification thanks to the nested division property of the floor function. So if $q(k)$ is the maximum number of questions required, we have the recursion:

\begin{align}

q(1) &= 0 \\

q(k) &= \left\lfloor{\tfrac{k-1}{2}}\right\rfloor + q\left( 1 + 2\left\lfloor{\tfrac{k-1}{4}}\right\rfloor \right)

\end{align}

If we define $f(k) = k-q(k+1)$ and do some rearrangements, we can write the following recursion for $f(k)$:

\begin{align}

f(0) &= 0 \\

f(k) &= k- \left\lfloor{\tfrac{k}{2}}\right\rfloor- 2\left\lfloor{\tfrac{k}{4}}\right\rfloor + f\left(1+2\left\lfloor{\tfrac{k}{4}}\right\rfloor\right)

\end{align}

It turns out that $f(k)$ is the number of 1’s in the binary representation of $k.$ Or, equivalently, $f(k)$ is the sum of the binary digits of $k$. I’ll omit the derivation, but you can work it out by writing $k$ in binary and noting that each operation does something simple to the digits. For example, if $x = abcde_2$ then $\lfloor x/2 \rfloor = abcd_2$. Therefore, we conclude that if there are $N$ generals, the number of questions required in order to ensure we can select a loyal general is:

$\displaystyle

q(N) = N-1-f(N-1)

$

where $f(k)$ is the number of 1’s in the binary representation of $k.$ Here is a plot showing the questions needed as a function of the number of generals.

We always require slightly fewer questions that there are generals, and we can bound the number of questions as:

\[

N-1-\log_2(N) \le q(N) \le N-2

\]