Sunday, September 7, 2025
HomeData Modelling & AILength of race track based on the final distance between participants

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.

Examples: 

Input: A = 11, B = 90, C = 80 
Output: 880

Input: A = 10, B = 20, C = 12 
Output: 60

Approach : 
Let X be the length of the race track.

Case 1: By the time when the First racer finished the race, the 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, the 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: 

C++




// C++ Program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
#define int long long
 
int32_t main()
{
    int A = 11;
    int B = 90;
    int C = 80;
 
    int ans = C * A;
    ans = ans / (C + A - B);
 
    cout << ans << endl;
}


Java




// Java Program for the
// above approach
 
import java.util.Scanner;
 
class GFG {
    public static void main(String args[])
    {
        int a = 11;
        int b = 90;
        int c = 80;
 
        System.out.println(c * a
                           / (c + a - b));
    }
}


Python3




# Python3 Program for the
# above approach
 
# Function to get the length
# of the race track
def findlength(a, b, c):
    # return the answer
    return c * a/(c + a-b)
 
a = 11
b = 90
c = 80
 
print(findlength(a, b, c))


C#




// C# program for the above approach
using System;
class GFG{
     
static void Main()
{
    int a = 11;
    int b = 90;
    int c = 80;
         
    Console.WriteLine(c * a / (c + a - b));
}
}
 
// This code is contributed by divyeshrabadiya07


Javascript




<script>
 
// JavaScript program for the above approach
   
// Driver Code
let a = 11;
let b = 90;
let c = 80;
 
document.write(c * a / (c + a - b));
 
// This code is contributed by sanjoy_62   
 
</script>


Output

880

Time Complexity: O(1)
Auxiliary Space: O(1)

Note: This is an interview question asked at POSTMAN (SDE Internship)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Dominic
32271 POSTS0 COMMENTS
Milvus
82 POSTS0 COMMENTS
Nango Kala
6642 POSTS0 COMMENTS
Nicole Veronica
11808 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11871 POSTS0 COMMENTS
Shaida Kate Naidoo
6755 POSTS0 COMMENTS
Ted Musemwa
7030 POSTS0 COMMENTS
Thapelo Manthata
6705 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS