Given three integers A, B and K. Initially, the first person was ahead of the second person by K kms. In every hour, the first person moves ahead by A kms and the second person moves ahead by B kms. The task is to print the number of hours after which the second person crosses the first. If it is impossible to do so then print -1.
Input: A = 4, B = 5, K = 1
Initially, the first person was ahead by 1 km.
After 1st hour the first and second person are at the same place.
After 2nd hour the first person moves ahead of the first person by 1 km.
Input: A = 6, B = 5, K = 1
A naive approach is to linearly check for every hour and print the n-th hour when the second person moves ahead of the first person.
An efficient approach is to solve the problem mathematically. The number of hours will be K / (B – A) + 1 when the second person moves ahead of the first person. Since you need to cover K kms, hence the time taken will be K / (B – A) where B – A is the speed of the second person with respect to the first person. If A ≥ B then it is not possible for the second person to cross the first person.
Below is the implementation of the above approach:
- Number of different positions where a person can stand
- Total number of days taken to complete the task if after certain days one person leaves
- Position of a person diametrically opposite on a circle
- Determine the position of the third person on regular N sided polygon
- Find speed of man from speed of stream and ratio of time with up and down streams
- Program to find the Speed of train as per speed of sound
- Total money to be paid after traveling the given number of hours
- Number of triangles after N moves
- Count number of 1s in the array after N moves
- Number of moves required to guess a permutation.
- Minimum number of moves to reach N starting from (1, 1)
- Minimum number of given moves required to make N divisible by 25
- Expected number of moves to reach the end of a board | Matrix Exponentiation
- Expected number of moves to reach the end of a board | Dynamic programming
- Minimum number of moves required to reach the destination by the king in a chess board
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.