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++
// 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; } |
Java
// 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 |
Python3
# 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 |
C#
// 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) |
PHP
<?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 ?> |
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++
// 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; } |
Java
// 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 |
Python3
# 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 |
C#
//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 |
PHP
<?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($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 ?> |
No
Recommended Posts:
- Find if two people ever meet after same number of jumps
- Check if it is possible to return to the starting position after moving in the given directions
- Check whether two points (x1, y1) and (x2, y2) lie on same side of a given line or not
- Check if the given 2-D points form T-shape or not
- How to check if given four points form a square
- Program to check whether 4 points in a 3-D plane are Coplanar
- Check whether it is possible to join two points given on circle such that distance between them is k
- Minimum number of points to be removed to get remaining points on one side of axis
- Steps required to visit M points in order on a circular ring of N points
- Prime points (Points that split a number into two primes)
- Probability that two persons will meet
- Time taken by two persons to meet on a circular track
- Time required to meet in equilateral triangle
- Program to check if the points are parallel to X axis or Y axis
- Number of Integral Points between Two Points
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.


