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.
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.
This article is contributed by Vishal Kumar Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
- Check if two people starting from different points ever meet
- Find the number of jumps to reach X in the number line from zero
- Maximum number of people that can be killed with strength P
- Minimum and Maximum number of pairs in m teams of n people
- Minimum number of jumps to reach end
- Number of jumps for a thief to cross walls
- Check if it is possible to reach a number by making jumps of two given length
- Probability that two persons will meet
- Time required to meet in equilateral triangle
- Time taken by two persons to meet on a circular track
- Distribute N candies among K people
- Count ways to distribute m items among n people
- Check if it is possible to move from (a, 0) to (b, 0) with given jumps
- Climb n-th stair with all jumps from 1 to n allowed (Three Different Approaches)
- Reach the numbers by making jumps of two given lengths