# Number of Distinct Meeting Points on a Circular Road

Consider two cars **A** and **B**, running infinitely (either clockwise or anti-clockwise) on a circular road. Given the speed of both the cars **a** and **b**. If a or b is positive, indicate they are moving in clockwise, Else they are moving in the anti-clockwise direction. The task is to find the number of distinct points they will meet each other at.

**Examples:**

Input: a = 1, b = -1Output: 2ExplanationCar A is moving clockwise while Car B is moving anti-clockwise but their speeds are same, so they will meet at two points i.e at the starting point and diametrically corresponding opposite point on the road.Input: a = 1, b = 2Output: 1

**Approach** :

Let the circumference of the circular road be **d**.

Let the time taken by cars **A** and **B** be **t _{a}** and

**t**respectively. Their relative speed is

_{b}**a – b**.

**A** and **B** start from the starting point and after some time, they will meet at starting point again. This time can be calculated by the **LCM** of **t _{a}** and

**t**. Within this time period, they may meet at certain points which needs to be found out. Observe that, after they meet at the starting point they keep on meeting at the same point.

_{b}Time taken to meet again at the starting point will be,

T_{1}= LCM(t_{a}, t_{b}) = LCM(d/a, d/b) = d/GCD(a, b)

Let them meet **N** times in the time period T_{1}.

So, the time delay between their consecutive meets is, say T_{2} can be calculated as,

T

_{2}= (T_{1}/ N).This time can be calculated by calculating the time taken to meet for the first time after they start.

So, the time taken by them to meet for the first time,

Therefore, T_{2}= (d / (a – b)).Dividing T

_{1}by T_{2}, we get,

N = (T_{1}/ T_{2}) = ((a – b) / GCD(a, b))

Below is implementation for the above approach:

## C++

`// CPP Program to find number of distinct point of meet on a circular road ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Returns the GCD of two number. ` `int` `gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `int` `c = a % b; ` ` ` `while` `(c != 0) { ` ` ` `a = b; ` ` ` `b = c; ` ` ` `c = a % b; ` ` ` `} ` ` ` `return` `b; ` `} ` ` ` `// Returns the number of distinct meeting points. ` `int` `numberOfmeet(` `int` `a, ` `int` `b) ` `{ ` ` ` `int` `ans; ` ` ` ` ` `// Find the relative speed. ` ` ` `if` `(a > b) ` ` ` `ans = a - b; ` ` ` `else` ` ` `ans = b - a; ` ` ` ` ` `// convert the negative value to positive. ` ` ` `if` `(a < 0) ` ` ` `a = a * (-1); ` ` ` ` ` `if` `(b < 0) ` ` ` `b = b * (-1); ` ` ` ` ` `return` `ans / gcd(a, b); ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `a = 1, b = -1; ` ` ` ` ` `cout << numberOfmeet(a, b) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java Program to find number ` `// of distinct point of meet ` `// on a circular road ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Returns the GCD ` `// of two number. ` `static` `int` `gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `int` `c = a % b; ` ` ` `while` `(c != ` `0` `) ` ` ` `{ ` ` ` `a = b; ` ` ` `b = c; ` ` ` `c = a % b; ` ` ` `} ` ` ` `return` `b; ` `} ` ` ` `// Returns the number of ` `// distinct meeting points. ` `static` `int` `numberOfmeet(` `int` `a, ` ` ` `int` `b) ` `{ ` ` ` `int` `ans; ` ` ` ` ` `// Find the relative speed. ` ` ` `if` `(a > b) ` ` ` `ans = a - b; ` ` ` `else` ` ` `ans = b - a; ` ` ` ` ` `// convert the negative ` ` ` `// value to positive. ` ` ` `if` `(a < ` `0` `) ` ` ` `a = a * (-` `1` `); ` ` ` ` ` `if` `(b < ` `0` `) ` ` ` `b = b * (-` `1` `); ` ` ` ` ` `return` `ans / gcd(a, b); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` `int` `a = ` `1` `, b = -` `1` `; ` ` ` `System.out.println(numberOfmeet(a, b)); ` `} ` `} ` ` ` `// This code is contributed by @ajit ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 Program to find ` `# number of distinct point ` `# of meet on a circular road ` `import` `math ` ` ` `# Returns the number of ` `# distinct meeting points. ` `def` `numberOfmeet(a, b): ` ` ` `ans ` `=` `0` `; ` ` ` ` ` `# Find the relative speed. ` ` ` `if` `(a > b): ` ` ` `ans ` `=` `a ` `-` `b; ` ` ` `else` `: ` ` ` `ans ` `=` `b ` `-` `a; ` ` ` ` ` `# convert the negative ` ` ` `# value to positive. ` ` ` `if` `(a < ` `0` `): ` ` ` `a ` `=` `a ` `*` `(` `-` `1` `); ` ` ` `if` `(b < ` `0` `): ` ` ` `b ` `=` `b ` `*` `(` `-` `1` `); ` ` ` `return` `int` `(ans ` `/` `math.gcd(a, b)); ` ` ` `# Driver Code ` `a ` `=` `1` `; ` `b ` `=` `-` `1` `; ` `print` `(numberOfmeet(a, b)); ` ` ` `# This code is contributed by mits ` |

*chevron_right*

*filter_none*

## C#

`// C# Program to find number ` `// of distinct point of meet ` `// on a circular road ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Returns the GCD ` `// of two number. ` `static` `int` `gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `int` `c = a % b; ` ` ` `while` `(c != 0) ` ` ` `{ ` ` ` `a = b; ` ` ` `b = c; ` ` ` `c = a % b; ` ` ` `} ` ` ` `return` `b; ` `} ` ` ` `// Returns the number of ` `// distinct meeting points. ` `static` `int` `numberOfmeet(` `int` `a, ` ` ` `int` `b) ` `{ ` ` ` `int` `ans; ` ` ` ` ` `// Find the relative speed. ` ` ` `if` `(a > b) ` ` ` `ans = a - b; ` ` ` `else` ` ` `ans = b - a; ` ` ` ` ` `// convert the negative ` ` ` `// value to positive. ` ` ` `if` `(a < 0) ` ` ` `a = a * (-1); ` ` ` ` ` `if` `(b < 0) ` ` ` `b = b * (-1); ` ` ` ` ` `return` `ans / gcd(a, b); ` `} ` ` ` `// Driver Code ` `static` `public` `void` `Main () ` `{ ` ` ` `int` `a = 1, b = -1; ` ` ` `Console.WriteLine( ` ` ` `numberOfmeet(a, b)); ` `} ` `} ` ` ` `// This code is contributed ` `// by @ajit ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP Program to find number ` `// of distinct point of meet ` `// on a circular road ` ` ` `// Returns the GCD of two number. ` `function` `gcd(` `$a` `, ` `$b` `) ` `{ ` ` ` `$c` `= ` `$a` `% ` `$b` `; ` ` ` `while` `(` `$c` `!= 0) ` ` ` `{ ` ` ` `$a` `= ` `$b` `; ` ` ` `$b` `= ` `$c` `; ` ` ` `$c` `= ` `$a` `% ` `$b` `; ` ` ` `} ` ` ` `return` `$b` `; ` `} ` ` ` `// Returns the number of ` `// distinct meeting points. ` `function` `numberOfmeet(` `$a` `, ` `$b` `) ` `{ ` ` ` `$ans` `; ` ` ` ` ` `// Find the relative speed. ` ` ` `if` `(` `$a` `> ` `$b` `) ` ` ` `$ans` `= ` `$a` `- ` `$b` `; ` ` ` `else` ` ` `$ans` `= ` `$b` `- ` `$a` `; ` ` ` ` ` `// convert the negative ` ` ` `// value to positive. ` ` ` `if` `(` `$a` `< 0) ` ` ` `$a` `= ` `$a` `* (-1); ` ` ` ` ` `if` `(` `$b` `< 0) ` ` ` `$b` `= ` `$b` `* (-1); ` ` ` ` ` `return` `$ans` `/ gcd(` `$a` `, ` `$b` `); ` `} ` ` ` `// Driver Code ` `$a` `= 1; ` `$b` `= -1; ` ` ` `echo` `numberOfmeet(` `$a` `, ` `$b` `).` `"\n"` `; ` ` ` `// This code is contributed by mits ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

2

## Recommended Posts:

- Steps required to visit M points in order on a circular ring of N points
- Count distinct points visited on the number line
- Prime points (Points that split a number into two primes)
- Number of Integral Points between Two Points
- Print a number strictly less than a given number such that all its digits are distinct.
- Number of quadrilaterals possible from the given points
- Check whether a number is circular prime or not
- Number of triangles that can be formed with given N points
- Maximum number of segments that can contain the given points
- Number of triangles formed from a set of points on three lines
- Number of triangles in a plane if no more than two points are collinear
- Print all combinations of points that can compose a given number
- Number of distinct subsets of a set
- Number of horizontal or vertical line segments to connect 3 points
- Number of distinct integers obtained by lcm(X, N)/X

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.