# Time taken by two persons to meet on a circular track

Given integers L, S1 and S2 where L is the length of a circular track in meters, S1 and S2 are the speeds of two persons in kilometers/hour moving in the same direction on the given track starting from the same starting point. The task is to find the following:

• The time after which they will meet for the first time.
• The time at which there are going to meet at the starting point.

Examples:

Input: L = 30, S1 = 5, S2 = 2
Output: Met first time after 10 hrs
Met at starting point after 30 hrs

Input: L = 10, S1 = 1, S2 = 2
Output: Met first time after 10 hrs
Met at starting point after 10 hrs

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

• For calculating the time at which they will first meet.
• First of all, calculate the Relative speed i.e. S1 – S2.
• Then use the formula, Time = Distance / Relative speed.
• For calculating the time at which they will again meet at starting point.
• First of all, calculate the time i.e. T1 and T2 which represent the time taken by both to cover 1 round of circular track by using the formula Time = Length of track / Speed.
• Then calculate the LCM to know the time they will again meet at starting point.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of above approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the time when both the ` `// persons will meet at the starting point ` `int` `startingPoint(``int` `Length, ``int` `Speed1, ``int` `Speed2) ` `{ ` `    ``int` `result1 = 0, result2 = 0; ` ` `  `    ``// Time to cover 1 round by both ` `    ``int` `time1 = Length / Speed1; ` `    ``int` `time2 = Length / Speed2; ` ` `  `    ``result1 = __gcd(time1, time2); ` ` `  `    ``// Finding LCM to get the meeting point ` `    ``result2 = time1 * time2 / (result1); ` ` `  `    ``return` `result2; ` `} ` ` `  `// Function to return the time when both ` `// the persons will meet for the first time ` `float` `firstTime(``int` `Length, ``int` `Speed1, ``int` `Speed2) ` `{ ` `    ``float` `result = 0; ` ` `  `    ``int` `relativeSpeed = ``abs``(Speed1 - Speed2); ` ` `  `    ``result = ((``float``)Length / relativeSpeed); ` ` `  `    ``return` `result; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `L = 30, S1 = 5, S2 = 2; ` ` `  `    ``// Calling function ` `    ``float` `first_Time = firstTime(L, S1, S2); ` `    ``int` `starting_Point = startingPoint(L, S1, S2); ` ` `  `    ``cout << ``"Met first time after "` `         ``<< first_Time << ``" hrs"` `<< endl; ` `    ``cout << ``"Met at starting point after "` `         ``<< starting_Point << ``" hrs"` `<< endl; ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of above approach ` `public` `class` `GFG { ` ` `  `// Function to return the time when both the ` `// persons will meet at the starting point ` `    ``static` `int` `startingPoint(``int` `Length, ``int` `Speed1, ``int` `Speed2) { ` `        ``int` `result1 = ``0``, result2 = ``0``; ` ` `  `        ``// Time to cover 1 round by both ` `        ``int` `time1 = Length / Speed1; ` `        ``int` `time2 = Length / Speed2; ` ` `  `        ``result1 = __gcd(time1, time2); ` ` `  `        ``// Finding LCM to get the meeting point ` `        ``result2 = time1 * time2 / (result1); ` ` `  `        ``return` `result2; ` `    ``} ` ` `  `    ``static` `int` `__gcd(``int` `a, ``int` `b) { ` `        ``if` `(b == ``0``) { ` `            ``return` `a; ` `        ``} ` `        ``return` `__gcd(b, a % b); ` ` `  `    ``} ` `// Function to return the time when both ` `// the persons will meet for the first time ` ` `  `    ``static` `float` `firstTime(``int` `Length, ``int` `Speed1, ``int` `Speed2) { ` `        ``float` `result = ``0``; ` ` `  `        ``int` `relativeSpeed = Math.abs(Speed1 - Speed2); ` ` `  `        ``result = ((``float``) Length / relativeSpeed); ` ` `  `        ``return` `result; ` `    ``} ` ` `  `// Driver Code ` `    ``public` `static` `void` `main(String[] args) { ` `        ``int` `L = ``30``, S1 = ``5``, S2 = ``2``; ` ` `  `        ``// Calling function ` `        ``float` `first_Time = firstTime(L, S1, S2); ` `        ``int` `starting_Point = startingPoint(L, S1, S2); ` ` `  `        ``System.out.println(``"Met first time after "` `                ``+ first_Time + ``" hrs"``); ` `        ``System.out.println(``"Met at starting point after "` `                ``+ starting_Point + ``" hrs"``); ` ` `  `    ``} ` `} `

## Python3

 `# Python 3 implementation of ` `# above approach ` ` `  `# import gcd() from math lib ` `from` `math ``import` `gcd ` ` `  `# Function to return the time when both the ` `# persons will meet at the starting point ` `def` `startingPoint(Length, Speed1, Speed2) : ` ` `  `    ``result1 ``=` `0` `    ``result2 ``=` `0` ` `  `    ``# Time to cover 1 round by both ` `    ``time1 ``=` `Length ``/``/` `Speed1 ` `    ``time2 ``=` `Length ``/``/` `Speed2 ` ` `  `    ``result1 ``=` `gcd(time1, time2) ` ` `  `    ``# Finding LCM to get the meeting point ` `    ``result2 ``=` `time1 ``*` `time2 ``/``/` `(result1) ` ` `  `    ``return` `result2 ` ` `  `# Function to return the time when both ` `# the persons will meet for the first time ` `def` `firstTime(Length, Speed1, Speed2) : ` ` `  `    ``result ``=` `0` ` `  `    ``relativeSpeed ``=` `abs``(Speed1 ``-` `Speed2) ` ` `  `    ``result ``=` `Length ``/` `relativeSpeed  ` ` `  `    ``return` `result ` ` `  `# Driver Code ` `if` `__name__ ``=``=` `"__main__"` `: ` `     `  `    ``L ``=` `30` `    ``S1 ``=` `5` `    ``S2 ``=` `2` ` `  `    ``# Calling function ` `    ``first_Time ``=` `firstTime(L, S1, S2) ` `    ``starting_Point ``=` `startingPoint(L, S1, S2) ` ` `  `    ``print``(``"Met first time after"``, first_Time, ``"hrs"``) ` `    ``print``(``"Met at starting point after"``,  ` `                  ``starting_Point, ``"hrs"``) ` ` `  `# This code is contributed by Ryuga `

## C#

 `// C# implementation of above approach  ` `using` `System;  ` ` `  `public` `class` `GFG { ` `  `  `// Function to return the time when both the ` `// persons will meet at the starting point ` `    ``static` `int` `startingPoint(``int` `Length, ``int` `Speed1, ``int` `Speed2) { ` `        ``int` `result1 = 0, result2 = 0; ` `  `  `        ``// Time to cover 1 round by both ` `        ``int` `time1 = Length / Speed1; ` `        ``int` `time2 = Length / Speed2; ` `  `  `        ``result1 = __gcd(time1, time2); ` `  `  `        ``// Finding LCM to get the meeting point ` `        ``result2 = time1 * time2 / (result1); ` `  `  `        ``return` `result2; ` `    ``} ` `  `  `    ``static` `int` `__gcd(``int` `a, ``int` `b) { ` `        ``if` `(b == 0) { ` `            ``return` `a; ` `        ``} ` `        ``return` `__gcd(b, a % b); ` `  `  `    ``} ` `// Function to return the time when both ` `// the persons will meet for the first time ` `  `  `    ``static` `float` `firstTime(``int` `Length, ``int` `Speed1, ``int` `Speed2) { ` `        ``float` `result = 0; ` `  `  `        ``int` `relativeSpeed = Math.Abs(Speed1 - Speed2); ` `  `  `        ``result = ((``float``) Length / relativeSpeed); ` `  `  `        ``return` `result; ` `    ``} ` `  `  `// Driver Code ` `    ``public` `static` `void` `Main() { ` `        ``int` `L = 30, S1 = 5, S2 = 2; ` `  `  `        ``// Calling function ` `        ``float` `first_Time = firstTime(L, S1, S2); ` `        ``int` `starting_Point = startingPoint(L, S1, S2); ` `  `  `        ``Console.WriteLine(``"Met first time after "` `                ``+ first_Time + ``" hrs"``); ` `        ``Console.WriteLine(``"Met at starting point after "` `                ``+ starting_Point + ``" hrs"``); ` `  `  `    ``} ` `} ` `/*This code is contributed by 29AjayKumar*/`

## PHP

 ` `

Output:

```Met first time after 10 hrs
Met at starting point after 30 hrs
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.