# Can you solve the fastest horse 🐴 algorithm problem?

### kapeel kokane ・1 min read

Do you think you can? (3 Part Series)

In this week's article, we dig into something different.

Let us look at an algorithm problem wherein there are multiple approaches but just one optimum solution.

### Problem Statement

💈💈💈💈💈💈💈💈💈💈💈💈💈💈💈

----------------------🐎

💈💈💈💈💈💈💈💈💈💈💈💈💈💈💈

------------🐎

💈💈💈💈💈💈💈💈💈💈💈💈💈💈💈

--------------------------🐎

💈💈💈💈💈💈💈💈💈💈💈💈💈💈💈

-----------------🐎

💈💈💈💈💈💈💈💈💈💈💈💈💈💈💈

--🐎

💈💈💈💈💈💈💈💈💈💈💈💈💈💈💈

There are 25 horses with you. The problem statement is to find the **top 3 fastest horses** among them. But there is a constraint. You have only 5 parallel race tracks and hence, you can only race 5 horses at a time.

How would you group the horses and race them so that you find out the top 3 fastest horses in the * minumum number of races*?

Give it a shot. After you think you have the answer, check out this video to see if you were correct:

Cheers!

Start by grouping them into 5 groups -

`a`

,`b`

,`c`

,`d`

and`e`

- and have each group race within itself. Let's say these are the results:Obviously any horse ranked fourth or fith within its own group cannot be in the top 3, leaving us with:

Now, pick the fastest horse of each group and let them race among each other. Without loss of generality, assume

`a1`

finished first, then`b1`

, then`c1`

, then`d1`

and finally`e1`

.`d1`

and`e1`

cannot be in the top 3, and we can also eliminate the rest of the`d`

and`e`

groups (which are slower than`d1`

and`e1`

):`c1`

cannot be the fastest horse, nor can it be the second fastest - at most it can be third, because`a1`

and`b1`

are faster than it. This means`c2`

and`c3`

can at most be fourth and fith - which we do not care about:Similarly, we can eliminate

`b3`

- but not`b2`

because it can still be third (if the first trio is`a1 b1 b2`

):This leaves us with 6 horses. But we already know that

`a1`

is the fastest of them all - so we can just make the other five race to determine second and third place.A total of 7 races.

OK, I watched the video and they used the same solution. I was hoping to also see a proof that this solution is indeed minimal - but there wasn't.

Can we prove this? I've given it some thought and I think I can prove that this is impossible with 6 or less races.

We will deal with different arrangements of the first 5 races:

The simplest case is where 19 or less horses participated in these first 5 races. This means we have only one more race and 6 or more horses, so at least one horse will not participate in any race - which is obviously a wrong solution.

The case where 20 horses participated in the first 5 races is also simple - if the final race will have these 5 horses, we will not be able to compare them to the 20 horses from the first 5 races, so this arrangement is also wrong.

The case where all 25 horses participated in the first 5 races. This means these 5 races were disjoint. Here we need to split the sixth race:

The case where between 21 and 24 horses participated in the first 5 races. Let N be the number of horses who did not participate. For each such horse, there is a link between two groups of 5 horses (one horse that participated in both). At best this link turns these two groups into a single group where we know the order (same horse finished last in one group and first in the other). This means that the horses from the first 5 races can be grouped to 5-N groups. Each of the horses that did not participate is a group of a single horse - so in total we have N+(5-N)=5 groups. Note that:

We have shown that with 6 races, not only is it impossible to determine the top 3 horses - it is also impossible to determine the top 2! I suspect the number 3 was chosen because you can't find more than that with only 7 races - but the proof for that is left as an exercise to the reader.

Given there is no constraints on what information we can gather from each race, and no constraints on assumptions. The only constraint is the number of tracks we have to use.

We can do this in five races providing we can time the races and each horse runs it's fastest every time it runs.

Split the horses into 5 groups and ensure that every horse runs at least once.

Time the horses and the 3 fastest times give you the 3 fastest horses.

I'm sorry, should have mentioned timing the horses was not an option 🙈

Yeah I was working outside of the spirit of the question, but these are the kinds of assumptions you should ask about in an interview when not specified, it shows an ability to think outside the box.

7 races? That doesn't seem right.

Split the 25 horses into 5 arbitrary groups of 5. For each group, determine the fastest horse. Race each group. You now have 5 horses, each the fastest of their group. Race those 5 horses to determine the fastest horse overall - a total of 6 races.

EDIT: I misread the question statement, so the above answer is incorrect. Sorry about that. I would still encourage you to solve my question down below (it's easy and fun!), though it gets a little trickier when you want to know the j fastest horses instead of just the fastest :)

Another fun, but simple exercise:

Find a closed formula that will answer this question for an arbitrary number of horses k and an arbitrary number of racetracks n.

Hint: Look at cases where n divides k first :)

I think this might be wrong.

Even if we take the winner of each group for a final "race of the champions", the winner of group 2 could be slower than the 2nd of group 1. So by eliminating 4 horses per each group during the first 5 races we might get a bad top 3.

7 seems to be the top solution.

But you need the top 3, not just the top 1.

Yes. Please read my post and learn from my silly mistakes :)

So each horse runs the given race in the exact same amount of time every time (individually, not collectively)? There exists absolutely zero overlap of +/- of average time between horses such that given the same two horses, horse one could beat horse two or just as easily vice versus? That’s the only way you can say the generalized solution holds. And once you realize that you understand how unrealistic the problem in general is. It’s just academic nonsense.

I remember going over this problem in class, it was a good time and we eventually came up with the same solution as stated by @idanarye . There was caveat that was brought up that should be mentioned and that's

we shouldn't have a way to track individual race horse times.Otherwise I could solve this problem by stating "time every horse using a stop watch", thus 5 races are the minimum to "get the speed" of every horse.

The stop-watch approach is 0 fun, but it is worth pointing out a missing constraint could totally change the problem.

I love puzzles/problems like this as they wreck your brain, and yet make your really think 😄

Here's the solution I thought of. Assuming we're not allowed to measure the speed and judge whether a horse is fast or not depending on whether it finishes first or not.

Conduct five races with groups of 5 horses and eliminate the last two finishers from each group, which will leave us with 15 horses. Repeat the process again - but this time it takes three races - and we are left with 9 horses. Repeat the process again - eliminate last 2 horses from the group of 5 and last 1 horse from thr group of 4 - and we're left with 6 horses, 2 races taken. And finally, take 5 from these, get the top 3 (1 race taken) and race it with the other one left and eliminate the last one leaving with 3 horses(another race taken).

Means a total of 12 races. Now lemme check the video...

Happy Coding!

Appears I actually got it right😅... Yeah tell me I looked at the solution or read it in the comments all you want...

Happy Coding🤗