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 = -1
Output : 2
Explanation
Car 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 = 2
Output : 1
Pre-Requisites: GCD | LCM
Approach :
Let the circumference of the circular road be d.
Let the time taken by cars A and B be ta and tb respectively. Their relative speed is 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 ta and tb. 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.
Time taken to meet again at the starting point will be,
T1 = LCM(ta, tb) = LCM(d/a, d/b) = d/GCD(a, b)
Let them meet N times in the time period T1.
So, the time delay between their consecutive meets is, say T2 can be calculated as,
T2 = (T1 / 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, T2 = (d / (a – b)).
Dividing T1 by T2, we get,
N = (T1 / T2) = ((a – b) / GCD(a, b))
Below is implementation for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
int c = a % b;
while (c != 0) {
a = b;
b = c;
c = a % b;
}
return b;
}
int numberOfmeet( int a, int b)
{
int ans;
if (a > b)
ans = a - b;
else
ans = b - a;
if (a < 0)
a = a * (-1);
if (b < 0)
b = b * (-1);
return ans / gcd(a, b);
}
int main()
{
int a = 1, b = -1;
cout << numberOfmeet(a, b) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG
{
static int gcd( int a, int b)
{
int c = a % b;
while (c != 0 )
{
a = b;
b = c;
c = a % b;
}
return b;
}
static int numberOfmeet( int a,
int b)
{
int ans;
if (a > b)
ans = a - b;
else
ans = b - a;
if (a < 0 )
a = a * (- 1 );
if (b < 0 )
b = b * (- 1 );
return ans / gcd(a, b);
}
public static void main (String[] args)
{
int a = 1 , b = - 1 ;
System.out.println(numberOfmeet(a, b));
}
}
|
Python3
import math
def numberOfmeet(a, b):
ans = 0 ;
if (a > b):
ans = a - b;
else :
ans = b - a;
if (a < 0 ):
a = a * ( - 1 );
if (b < 0 ):
b = b * ( - 1 );
return int (ans / math.gcd(a, b));
a = 1 ;
b = - 1 ;
print (numberOfmeet(a, b));
|
C#
using System;
class GFG
{
static int gcd( int a, int b)
{
int c = a % b;
while (c != 0)
{
a = b;
b = c;
c = a % b;
}
return b;
}
static int numberOfmeet( int a,
int b)
{
int ans;
if (a > b)
ans = a - b;
else
ans = b - a;
if (a < 0)
a = a * (-1);
if (b < 0)
b = b * (-1);
return ans / gcd(a, b);
}
static public void Main ()
{
int a = 1, b = -1;
Console.WriteLine(
numberOfmeet(a, b));
}
}
|
PHP
<?php
function gcd( $a , $b )
{
$c = $a % $b ;
while ( $c != 0)
{
$a = $b ;
$b = $c ;
$c = $a % $b ;
}
return $b ;
}
function numberOfmeet( $a , $b )
{
$ans ;
if ( $a > $b )
$ans = $a - $b ;
else
$ans = $b - $a ;
if ( $a < 0)
$a = $a * (-1);
if ( $b < 0)
$b = $b * (-1);
return $ans / gcd( $a , $b );
}
$a = 1;
$b = -1;
echo numberOfmeet( $a , $b ). "\n" ;
?>
|
Javascript
<script>
function gcd(a, b)
{
var c = a % b;
while (c != 0) {
a = b;
b = c;
c = a % b;
}
return b;
}
function numberOfmeet(a, b)
{
var ans;
if (a > b)
ans = a - b;
else
ans = b - a;
if (a < 0)
a = a * (-1);
if (b < 0)
b = b * (-1);
return ans / gcd(a, b);
}
var a = 1, b = -1;
document.write( numberOfmeet(a, b));
</script>
|
Output:
2
Last Updated :
04 May, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...