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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find if two people
// starting from different positions
// ever meet or not.
#include <bits/stdc++.h>
using namespace std;
  
bool everMeet(int x1, int x2, int v1, int v2)
{
    // If speed of a person at a position before
    // other person is smaller, then return false.
    if (x1 < x2 && v1 < v2)
       return false;
    if (x1 > x2 && v1 > v2)
       return false;  
    
    // Making sure that x1 is greater
    if (x1 < x2)
    {
        swap(x1, x2);
        swap(v1, v2);
    }    
  
     // Until one person crosses other
     while (x1 >= x2) {
        if (x1 == x2) 
            return true;
          
        // first person taking one
        // jump in each iteration
        x1 = x1 + v1; 
          
        // second person taking
        // one jump in each iteration
        x2 = x2 + v2; 
    }
  
    return false;   
}
  
// Driver code
int main()
{
    int x1 = 5, v1 = 8, x2 = 4, v2 = 7;
    if (everMeet(x1, x2, v1, v2))
        printf("Yes");    
    else 
        printf("No");
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find 
// if two people starting 
// from different positions
// ever meet or not.
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 speed of a person 
        // at a position before 
        // other person is smaller, 
        // then return false.
        if (x1 < x2 && v1 < v2)
        return false;
        if (x1 > x2 && v1 > v2)
        return false
          
        // Making sure that 
        // x1 is greater
        if (x1 < x2)
        {
            swap(x1, x2);
            swap(v1, v2);
        
      
        // Until one person 
        // crosses other
        while (x1 >= x2) 
        {
            if (x1 == x2) 
                return true;
              
            // first person taking one
            // jump in each iteration
            x1 = x1 + v1; 
              
            // second person taking
            // one jump in each iteration
            x2 = x2 + v2; 
        }
      
        return false
    }
      
    // Driver code
    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");
    }
}
  
// This code is contributed 
// by akt_mit 

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find if two 
# people starting from different 
# positions ever meet or not.
  
def everMeet(x1, x2, v1, v2):
  
    # If speed of a person at 
    # a position before other
    # person is smaller, then 
    # return false.
    if (x1 < x2 and v1 < v2):
        return False;
    if (x1 > x2 and v1 > v2):
        return False
  
    # Making sure that
    # x1 is greater
    if (x1 < x2):
  
        x1, x2 = x2,x1;
        v1, v2 = v2,v1;
      
  
    # Until one person
    # crosses other
    while (x1 >= x2): 
      
        if (x1 == x2): 
            return True;
          
        # first person taking one
        # jump in each iteration
        x1 = x1 + v1; 
          
        # second person taking
        # one jump in each iteration
        x2 = x2 + v2; 
      
  
    return False
  
  
# Driver code
x1 = 5;
v1 = 8;
x2 = 4;
v2 = 7;
if (everMeet(x1, x2,v1, v2)):
    print("Yes"); 
else:
    print("No");
  
# This code is contributed by mits

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find if two 
// people starting from different 
// positions ever meet or not.
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 speed of a person at a 
        // position before other person
        // is smaller, then return false.
        if (x1 < x2 && v1 < v2)
        return false;
        if (x1 > x2 && v1 > v2)
        return false
          
        // Making sure that x1 is greater
        if (x1 < x2)
        {
            swap(ref x1, ref x2);
            swap(ref v1, ref v2);
        
      
        // Until one person crosses other
        while (x1 >= x2) 
        {
            if (x1 == x2) 
                return true;
              
            // first person taking one
            // jump in each iteration
            x1 = x1 + v1; 
              
            // second person taking
            // one jump in each iteration
            x2 = x2 + v2; 
        }
      
        return false
    }
      
    // Driver code
    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");
    }
}
  
// This code is contributed by 
// Manish Shaw(manishshaw1)

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to find if two 
// people starting from different 
// positions ever meet or not.
  
function everMeet($x1, $x2
                  $v1, $v2)
{
    // If speed of a person at 
    // a position before other
    // person is smaller, then 
    // return false.
    if ($x1 < $x2 && $v1 < $v2)
        return false;
    if ($x1 > $x2 && $v1 > $v2)
        return false; 
  
    // Making sure that
    // x1 is greater
    if ($x1 < $x2)
    {
        list($x1, $x2) = array($x2
                               $x1);
        list($v1, $v2) = array($v2
                               $v1);
      
    
  
    // Until one person
    // crosses other
    while ($x1 >= $x2
    {
        if ($x1 == $x2
            return true;
          
        // first person taking one
        // jump in each iteration
        $x1 = $x1 + $v1
          
        // second person taking
        // one jump in each iteration
        $x2 = $x2 + $v2
    }
  
    return false; 
}
  
// Driver code
$x1 = 5;
$v1 = 8;
$x2 = 4;
$v2 = 7;
if (everMeet($x1, $x2,
             $v1, $v2))
    echo "Yes"
else
    echo "No";
  
// This code is contributed by ajit
?>

chevron_right


Output:

No

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)
Imlementation is given below:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find if two people
// starting from different positions
// ever meet or not.
#include <bits/stdc++.h>
using namespace std;
  
bool everMeet(int x1, int x2, int v1, int v2)
{
  
    // If speed of a person at a position before
    // other person is smaller, then return false.
    if (x1 < x2 && v1 < v2)
       return false;
    if (x1 > x2 && v1 > v2)
       return false;  
  
    // Making sure that x1 is greater
    if (x1 < x2)
    {
        swap(x1, x2);
        swap(v1, v2);
    }    
  
    // checking if relative speed is
    // a factor of relative distance or not
    return ((x1 - x2) % (v1 - v2) == 0); 
}
  
// Driver code
int main()
{
    int x1 = 5, v1 = 8, x2 = 4, v2 = 7;
    if (everMeet(x1, x2, v1, v2))
        printf("Yes");    
    else 
        printf("No");
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find if two people 
// starting from different positions 
// ever meet or not. 
  
public class GFG {
  
    static boolean everMeet(int x1, int x2, int v1, int v2) {
  
        // If speed of a person at a position before 
        // other person is smaller, then return false. 
        if (x1 < x2 && v1 < v2) {
            return false;
        }
        if (x1 > x2 && v1 > v2) {
            return false;
        }
  
        // Making sure that x1 is greater 
        if (x1 < x2) {
            swap(x1, x2);
            swap(v1, v2);
        }
  
        // checking if relative speed is 
        // a factor of relative distance or not 
        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");
        }
    }
}
  
// This code is contributed by 29AjayKumar 

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

      
# Python3 program to find if two people
# starting from different positions
# ever meet or not.
  
def everMeet(x1, x2, v1, v2):
  
    # If speed of a person at a position before
    # other person is smaller, then return false.
    if (x1 < x2 and v1 < v2):
        return False;
        if (x1 > x2 and v1 > v2):
            return False
              
            # Making sure that x1 is greater
            if (x1 < x2):
                swap(x1, x2)
                swap(v1, v2)
                  
                # checking if relative speed is
                # a factor of relative distance or not
                return ((x1 - x2) % (v1 - v2) == 0
                def swap(a, b):
                    t = a
                    a = b
                    b = t
# Driver code
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()
      
      
# This code is contributed by 29AjayKumar

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

//C# program to find if two people 
// starting from different positions 
// ever meet or not. 
using System; 
public class GFG{
  
  
    static bool everMeet(int x1, int x2, int v1, int v2) { 
  
        // If speed of a person at a position before 
        // other person is smaller, then return false. 
        if (x1 < x2 && v1 < v2) { 
            return false
        
        if (x1 > x2 && v1 > v2) { 
            return false
        
  
        // Making sure that x1 is greater 
        if (x1 < x2) { 
            swap(x1, x2); 
            swap(v1, v2); 
        
  
        // checking if relative speed is 
        // a factor of relative distance or not 
        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"); 
        
    
  
  
// This code is contributed by 29AjayKumar

chevron_right


PHP

$x2 && $v1 > $v2)
return false;

// Making sure that x1 is greater
if ($x1 < $x2) { list($x1, $x2) = array($x2, $x1); list($v2, $v1) = array($v1, $v2); } // checking if relative speed is // a factor of relative distance or not return (($x1 - $x2) % ($v1 - $v2) == 0); } // Driver code $x1 = 5; $v1 = 8; $x2 = 4; $v2 = 7; if (everMeet($x1, $x2, $v1, $v2)) print("Yes"); else print("No"); // This code is contributed by mits ?>

Output :

No


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.