Find if two people ever meet after same number of jumps - GeeksforGeeks

29 Apr, 2021
Two people start races from two different points p1 and p2. They cover s1 and s2 meters in a jump. Find if they will ever meet at a point after the same number of jumps.Examples:  
Input : p1 = 6, s1 = 3, 
        p2 = 8, s2 = 2
Output : Yes
Explanation: 6->9->12
             8->10->12
They meet after two jumps.

Input : p1 = 4, s1 = 4, 
        p2 = 8, s2 = 2
Output : Yes
Explanation: 4->8->12
             8->10->12

Input : p1 = 0, s1 = 2, 
        p2 = 5, s2 = 3
Output : No

Input : p1 = 42, s1 = 3, 
        p2 = 94, s2 = 2
Output : Yes
 
A simple solution is to make them jump one by one. After every jump, see if they are same point or not.An efficient solution is based on below facts: Since starting points are always different, they will meet if following conditions are met. (1) Speeds are not same (2) Difference between speeds divide the total distance between initial points.  
C++
Java
Python3
C#
PHP
Javascript
// C++ program to find any one of them// can overtake the other#include<bits/stdc++.h>using namespace std; // function to find if any one of them can// overtake the otherbool sackRace(int p1, int s1, int p2, int s2){     // Since starting points are always    // different, they will meet if following    // conditions are met.    // (1) Speeds are not same    // (2) Difference between speeds divide the    //     total distance between initial points.       return ( (s1 > s2 && (p2 - p1) % (s1 - s2) == 0) ||             (s2 > s1 && (p1 - p2) % (s2 - s1) == 0));} // driver programint main(){    int p1 = 4, s1 = 4, p2 = 8, s2 = 2;    sackRace(p1, s1, p2, s2)? cout << "Yes\n" :                              cout << "No\n";    return 0;}
// java program to find any one of them// can overtake the otherimport java.util.Arrays; public class GFG {         // function to find if any one of    // them can overtake the other    static boolean sackRace(int p1, int s1,                            int p2, int s2)    {             // Since starting points are        // always different, they will        // meet if following conditions        // are met.        // (1) Speeds are not same        // (2) Difference between speeds        // divide the total distance        // between initial points.        return ( (s1 > s2 && (p2 - p1) %                    (s1 - s2) == 0) ||                    (s2 > s1 && (p1 - p2)                    % (s2 - s1) == 0));    }         public static void main(String args[])    {        int p1 = 4, s1 = 4, p2 = 8, s2 = 2;                 if(sackRace(p1, s1, p2, s2))            System.out.println("Yes" );        else            System.out.println("No");     }} // This code is contributed by Sam007.
# python program to find any one of them# can overtake the other # function to find if any one of them can# overtake the otherdef sackRace(p1, s1, p2, s2):         # Since starting points are always    # different, they will meet if following    # conditions are met.    # (1) Speeds are not same    # (2) Difference between speeds divide the    #     total distance between initial points.    return ( (s1 > s2 and (p2 - p1) % (s1 - s2) == 0)         or (s2 > s1 and (p1 - p2) % (s2 - s1) == 0))  # driver programp1 = 4s1 = 4p2 = 8s2 = 2if(sackRace(p1, s1, p2, s2)):    print("Yes")else:    print("No")     # This code is contributed by Sam007
// C# program to find any one of them// can overtake the otherusing System; class GFG {         // function to find if any one of    // them can overtake the other    static bool sackRace(int p1, int s1,                          int p2, int s2)    {             // Since starting points are        // always different, they will        // meet if following conditions        // are met.        // (1) Speeds are not same        // (2) Difference between speeds        // divide the total distance        // between initial points.        return ( (s1 > s2 && (p2 - p1) %                       (s1 - s2) == 0) ||                    (s2 > s1 && (p1 - p2)                       % (s2 - s1) == 0));    }         // Driver code    public static void Main()    {        int p1 = 4, s1 = 4, p2 = 8,        s2 = 2;                 if(sackRace(p1, s1, p2, s2))            Console.WriteLine("Yes" );        else            Console.WriteLine("No");                                     }} // This code is contributed by Sam007.
<?php// PHP program to find any one of them// can overtake the other // function to find if any one of// them can overtake the otherfunction sackRace($p1, $s1, $p2, $s2){     // Since starting points are always    // different, they will meet if following    // conditions are met.    // (1) Speeds are not same    // (2) Difference between speeds divide the    //     total distance between initial points.    return (($s1 > $s2 && ($p2 - $p1) % ($s1 - $s2) == 0) ||            ($s2 > $s1 && ($p1 - $p2) % ($s2 - $s1) == 0));}     // Driver Code    $p1 = 4;    $s1 = 4;    $p2 = 8;    $s2 = 2;    if(sackRace($p1, $s1, $p2, $s2))        echo "Yes\n" ;    else        echo "No\n";         // This code is contributed by Sam007?>
<script> // JavaScript program to find any one of them// can overtake the other     // function to find if any one of    // them can overtake the other    function sackRace(p1, s1, p2, s2)    {               // Since starting points are        // always different, they will        // meet if following conditions        // are met.        // (1) Speeds are not same        // (2) Difference between speeds        // divide the total distance        // between initial points.        return ( (s1 > s2 && (p2 - p1) %                    (s1 - s2) == 0) ||                    (s2 > s1 && (p1 - p2)                    % (s2 - s1) == 0));    } // Driver Code        let p1 = 4, s1 = 4, p2 = 8, s2 = 2;                   if(sackRace(p1, s1, p2, s2))            document.write("Yes" );        else            document.write("No"); // This code is contributed by susmitakundugoaldanga.</script>
Output :  
Yes
This article is contributed by Vishal Kumar Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
Sam007
Akanksha_Rai
susmitakundugoaldanga
Mathematical
School Programming
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Modulo Operator (%) in C/C++ with Examples
Merge two sorted arrays
Prime Numbers
Program to find sum of elements in a given array
Program for factorial of a number
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
Interfaces in Java