Check if two people starting from different points ever meet
There are two people that start from two different positions, let’s say x1 and x2. Both can jump v1 and v2 meters ahead respectively. We have to find if both will ever meet given that the number of jumps taken by both has to be same.
Print ‘Yes’ if they will meet,
print ‘No’ they will not.
Examples :
Input : x1 = 5, v1 = 8, x2 = 4, v2 = 7
Output : No
Explanation: The first person is starting ahead of the second one.
and his speed is also greater than the second one, so they will never meet.
Input : x1 = 6, v1 = 6, x2 = 4, v2 = 8
Output : Yes
Naive Approach: In this, we calculate the position of each person after each jump and checks if they have landed in the same spot or not. This one having complexity O(n).
C++
#include <bits/stdc++.h>
using namespace std;
bool everMeet( int x1, int x2, int v1, int v2)
{
if (x1 < x2 && v1 <= v2)
return false ;
if (x1 > x2 && v1 >=v2)
return false ;
if (x1 < x2)
{
swap(x1, x2);
swap(v1, v2);
}
while (x1 >= x2) {
if (x1 == x2)
return true ;
x1 = x1 + v1;
x2 = x2 + v2;
}
return false ;
}
int main()
{
int x1 = 5, v1 = 8, x2 = 4, v2 = 7;
if (everMeet(x1, x2, v1, v2))
printf ( "Yes" );
else
printf ( "No" );
return 0;
}
|
Java
import java.io.*;
class GFG
{
static void swap( int a, int b)
{
int t = a;
a = b;
b = t;
}
static boolean everMeet( int x1, int x2,
int v1, int v2)
{
if (x1 < x2 && v1 <= v2)
return false ;
if (x1 > x2 && v1 >= v2)
return false ;
if (x1 < x2)
{
swap(x1, x2);
swap(v1, v2);
}
while (x1 >= x2)
{
if (x1 == x2)
return true ;
x1 = x1 + v1;
x2 = x2 + v2;
}
return false ;
}
public static void main (String[] args)
{
int x1 = 5 , v1 = 8 ,
x2 = 4 , v2 = 7 ;
if (everMeet(x1, x2, v1, v2))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def everMeet(x1, x2, v1, v2):
if (x1 < x2 and v1 < = v2):
return False ;
if (x1 > x2 and v1 > = v2):
return False ;
if (x1 < x2):
x1, x2 = x2,x1;
v1, v2 = v2,v1;
while (x1 > = x2):
if (x1 = = x2):
return True ;
x1 = x1 + v1;
x2 = x2 + v2;
return False ;
x1 = 5 ;
v1 = 8 ;
x2 = 4 ;
v2 = 7 ;
if (everMeet(x1, x2,v1, v2)):
print ( "Yes" );
else :
print ( "No" );
|
C#
using System;
class GFG
{
static void swap( ref int a,
ref int b)
{
int t = a;
a = b;
b = t;
}
static bool everMeet( int x1, int x2,
int v1, int v2)
{
if (x1 < x2 && v1 <= v2)
return false ;
if (x1 > x2 && v1 >= v2)
return false ;
if (x1 < x2)
{
swap( ref x1, ref x2);
swap( ref v1, ref v2);
}
while (x1 >= x2)
{
if (x1 == x2)
return true ;
x1 = x1 + v1;
x2 = x2 + v2;
}
return false ;
}
static void Main()
{
int x1 = 5, v1 = 8,
x2 = 4, v2 = 7;
if (everMeet(x1, x2, v1, v2))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
PHP
<?php
function everMeet( $x1 , $x2 ,
$v1 , $v2 )
{
if ( $x1 < $x2 && $v1 <= $v2 )
return false;
if ( $x1 > $x2 && $v1 >= $v2 )
return false;
if ( $x1 < $x2 )
{
list( $x1 , $x2 ) = array ( $x2 ,
$x1 );
list( $v1 , $v2 ) = array ( $v2 ,
$v1 );
}
while ( $x1 >= $x2 )
{
if ( $x1 == $x2 )
return true;
$x1 = $x1 + $v1 ;
$x2 = $x2 + $v2 ;
}
return false;
}
$x1 = 5;
$v1 = 8;
$x2 = 4;
$v2 = 7;
if (everMeet( $x1 , $x2 ,
$v1 , $v2 ))
echo "Yes" ;
else
echo "No" ;
?>
|
Javascript
<script>
function swap(a, b)
{
let t = a;
a = b;
b = t;
}
function everMeet(x1, x2, v1, v2)
{
if (x1 < x2 && v1 <= v2)
return false ;
if (x1 > x2 && v1 >= v2)
return false ;
if (x1 < x2)
{
swap(x1, x2);
swap(v1, v2);
}
while (x1 >= x2)
{
if (x1 == x2)
return true ;
x1 = x1 + v1;
x2 = x2 + v2;
}
return false ;
}
let x1 = 5, v1 = 8, x2 = 4, v2 = 7;
if (everMeet(x1, x2, v1, v2))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Auxiliary Space: O(1) because it is using constant space for variables
Efficient approach: Other way is to use the concept of Relative Speed. If the X1 is D distance away from X2, then relative speed of X1 should be in factors of D so as both land on the same spot.
Example:
X1 = 10
X2 = 25
V1 = 10
V2 = 8
Since here D = 15 and relative speed = 2 which is not a factor of D they will never meet.
The time complexity of this algorithm is O(1)
Implementation is given below:
C++
#include <bits/stdc++.h>
using namespace std;
bool everMeet( int x1, int x2, int v1, int v2)
{
if (x1 < x2 && v1 <= v2)
return false ;
if (x1 > x2 && v1 >= v2)
return false ;
if (x1 < x2)
{
swap(x1, x2);
swap(v1, v2);
}
return ((x1 - x2) % (v1 - v2) == 0);
}
int main()
{
int x1 = 5, v1 = 8, x2 = 4, v2 = 7;
if (everMeet(x1, x2, v1, v2))
printf ( "Yes" );
else
printf ( "No" );
return 0;
}
|
Java
public class GFG {
static boolean everMeet( int x1, int x2, int v1, int v2) {
if (x1 < x2 && v1 <= v2) {
return false ;
}
if (x1 > x2 && v1 >= v2) {
return false ;
}
if (x1 < x2) {
swap(x1, x2);
swap(v1, v2);
}
return ((x1 - x2) % (v1 - v2) == 0 );
}
static void swap( int a, int b) {
int t = a;
a = b;
b = t;
}
public static void main(String[] args) {
int x1 = 5 , v1 = 8 , x2 = 4 , v2 = 7 ;
if (everMeet(x1, x2, v1, v2)) {
System.out.printf( "Yes" );
} else {
System.out.printf( "No" );
}
}
}
|
Python3
def everMeet(x1, x2, v1, v2):
if (x1 < x2 and v1 < = v2):
return False ;
if (x1 > x2 and v1 > = v2):
return False ;
if (x1 < x2):
swap(x1, x2)
swap(v1, v2)
return ((x1 - x2) % (v1 - v2) = = 0 )
def swap(a, b):
t = a
a = b
b = t
def main():
x1, v1, x2, v2 = 5 , 8 , 4 , 7
if (everMeet(x1, x2, v1, v2)):
print ( "Yes" )
else :
print ( "No" )
if __name__ = = '__main__' :
main()
|
C#
using System;
public class GFG{
static bool everMeet( int x1, int x2, int v1, int v2) {
if (x1 < x2 && v1 <= v2) {
return false ;
}
if (x1 > x2 && v1 >= v2) {
return false ;
}
if (x1 < x2) {
swap(x1, x2);
swap(v1, v2);
}
return ((x1 - x2) % (v1 - v2) == 0);
}
static void swap( int a, int b) {
int t = a;
a = b;
b = t;
}
public static void Main() {
int x1 = 5, v1 = 8, x2 = 4, v2 = 7;
if (everMeet(x1, x2, v1, v2)) {
Console.WriteLine( "Yes" );
} else {
Console.WriteLine( "No" );
}
}
}
|
PHP
<?php
function everMeet( $x1 , $x2 , $v1 , $v2 )
{
if ( $x1 < $x2 && $v1 <= $v2 )
return false;
if ( $x1 > $x2 && $v1 >= $v2 )
return false;
if ( $x1 < $x2 )
{
list( $x1 , $x2 ) = array ( $x2 , $x1 );
list( $v2 , $v1 ) = array ( $v1 , $v2 );
}
return (( $x1 - $x2 ) % ( $v1 - $v2 ) == 0);
}
$x1 = 5; $v1 = 8;
$x2 = 4; $v2 = 7;
if (everMeet( $x1 , $x2 , $v1 , $v2 ))
print ( "Yes" );
else
print ( "No" );
?>
|
Javascript
<script>
function everMeet(x1, x2, v1, v2)
{
if (x1 < x2 && v1 <= v2)
{
return false ;
}
if (x1 > x2 && v1 >= v2)
{
return false ;
}
if (x1 < x2)
{
swap(x1, x2);
swap(v1, v2);
}
return ((x1 - x2) % (v1 - v2) == 0);
}
function swap(a, b)
{
let t = a;
a = b;
b = t;
}
let x1 = 5, v1 = 8, x2 = 4, v2 = 7;
if (everMeet(x1, x2, v1, v2))
{
document.write( "Yes" );
}
else
{
document.write( "No" );
}
</script>
|
Time complexity: O(1)
Auxiliary space: O(1)
Last Updated :
02 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...