Length of race track based on the final distance between participants
Given three integers A, B and C, the task is to find the length of a race track if 3 racers are competing in a race where the first racer beats the second racer by A meters, the first racer beats the third racer by B meters and the second racer beats the third by C meters.
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
Input: A = 11, B = 90, C = 80
Input: A = 10, B = 20, C = 12
Let X be the length of the race track.
Case 1: By the time when the First racer finished the race, distances covered by all the 3 racers are:
First = X, Second = X – A, Third = X – B
Let the time taken by the First racer to finish the race be T1.
Case 2: By the time when the Second racer finished the race, distances covered by the remaining 2 racers are:
Second = X, Third = X – C
Let the time taken by the Second racer to finish the race be T2.
The ratio of the speeds of the Second and the Third racer will be constant in both case 1 and case 2 which implies:
=> ((X – A) / T1) / ((X – B) / T1) = (X / T2) / ((X – C) / T2)
=> (X – A) / (X – B) = (X) / (X – C)
=> X2 – A*X – C*X + A*C = X2 – B*X
=> A*C = (C + A – B)*X
=> X = A*C / (C + A – B)
Below is the implementation of the above program:
Note: This is an interview question asked at POSTMAN (SDE Internship)