(This is actually a coding challenge for an interview. Unfortunately, only 2 test cases are coded, so even if my solution passes them both, that doesn't tell me I've found the correct solution. Also, not knowing the correct solution, I can't write test cases myself.)
Here's one example for a possible set of $N = 4$ people. The heights could be $$ h = \{5, 10, 6, 8\} $$
A possible solution is sitting people such that the person tall 5 has person tall 6 immediately to their left, and person tall 8 immediately to their right; then person tall 10 would be opposite to person tall 5; the maximum difference in height is between person tall 6 and person tall 10, and it's equal to 4. A better solution cannot be found for such an example.
But what in the general case?
This is my reasoning.
- If the table was not circular, I could sit all people sorted from shortest to tallest, and that would minimize the maximum difference between any two adjacent people.
- But the table is circular, so such an arrangement doesn't work because the tallest and shortest persons can be beside each other (doing so would maximise the maximum difference), at least one person has to be between them, and possibly more.
At this stage of my reasoning I thought that the "round" table can be thought of like this:
T (tallest person)
+---+
| |
| |
some | | some
people | | people
here | | here
| |
| |
+---+
S (shortest person)
- Whichever the arrangement is, the heights should increase when going from the shortest to the tallest person, whether we go clockwise (left side in the sketch above) or counterclockwise (right side in the sketch above).
I'm pretty sure about this last point, and I think it also guarantees that any solution strategy cannot be cheaper than the cheapest way to sort the whole list of people. As in, the cost of the optimal algorithm is at least the cost of the cheapest sorting algorithm.
- Therefore a strategy to the solution seems to consist in sorting people by height, sitting them like this
and then moving a selection of those on the left side to the right side without changing their relative order.T (h1, tallest person) +---+ h2 | | h3 | | | | ... | | none here for now | | h(n-2)| | h(n-1)| | +---+ S (hn, shortest person)
But how do I make the choice of which ones I should move to the other side?
Here I have the feeling that moving odd-numbered people (or, instead, even-numbered people) to the other side of the table gives a solution.
But I'm not sure.