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**.

**Examples:**

Input:A = 4, B = 5, K = 1

Output:2

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

Output:-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:

## C++

`// C++ implementation of the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the number of ` `// hours for the second person to move ahead ` `int` `findHours(` `int` `a, ` `int` `b, ` `int` `k) ` `{ ` ` ` `if` `(a >= b) ` ` ` `return` `-1; ` ` ` ` ` `// Time taken to equalize ` ` ` `int` `time` `= k / (b - a); ` ` ` ` ` `// Time taken to move ahead ` ` ` `time` `= ` `time` `+ 1; ` ` ` ` ` `return` `time` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `a = 4, b = 5, k = 1; ` ` ` `cout << findHours(a, b, k); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the above approach ` `import` `java.io.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the number of ` `// hours for the second person to move ahead ` `static` `int` `findHours(` `int` `a, ` `int` `b, ` `int` `k) ` `{ ` ` ` `if` `(a >= b) ` ` ` `return` `-` `1` `; ` ` ` ` ` `// Time taken to equalize ` ` ` `int` `time = k / (b - a); ` ` ` ` ` `// Time taken to move ahead ` ` ` `time = time + ` `1` `; ` ` ` ` ` `return` `time; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main (String[] args) ` `{ ` ` ` ` ` `int` `a = ` `4` `, b = ` `5` `, k = ` `1` `; ` ` ` `System.out.println (findHours(a, b, k)); ` `} ` `} ` ` ` `// The code is contributed by ajit..@23 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the above approach ` ` ` `# Function to return the number of ` `# hours for the second person to move ahead ` `def` `findHours(a, b, k): ` ` ` `if` `(a >` `=` `b): ` ` ` `return` `-` `1` ` ` ` ` `# Time taken to equalize ` ` ` `time ` `=` `k ` `/` `/` `(b ` `-` `a) ` ` ` ` ` `# Time taken to move ahead ` ` ` `time ` `=` `time ` `+` `1` ` ` ` ` `return` `time ` ` ` ` ` `# Driver code ` ` ` `a ` `=` `4` `b ` `=` `5` `k ` `=` `1` `print` `(findHours(a, b, k)) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the above approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the number of ` `// hours for the second person to move ahead ` `static` `int` `findHours(` `int` `a, ` `int` `b, ` `int` `k) ` `{ ` ` ` `if` `(a >= b) ` ` ` `return` `-1; ` ` ` ` ` `// Time taken to equalize ` ` ` `int` `time = k / (b - a); ` ` ` ` ` `// Time taken to move ahead ` ` ` `time = time + 1; ` ` ` ` ` `return` `time; ` `} ` ` ` `// Driver code ` `static` `public` `void` `Main () ` `{ ` ` ` `int` `a = 4, b = 5, k = 1; ` ` ` `Console.Write(findHours(a, b, k)); ` `} ` `} ` ` ` `// The code is contributed by ajit. ` |

*chevron_right*

*filter_none*

**Output:**

2

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find speed of man from speed of stream and ratio of time with up and down streams
- Count possible moves in the given direction in a grid
- Program to find Length of Bridge using Speed and Length of Train
- Find First element in AP which is multiple of given prime
- Total position where king can reach on a chessboard in exactly M moves | Set 2
- Find the modified array after performing k operations of given type
- Number of odd and even results for every value of x in range [min, max] after performing N steps
- Element which occurs consecutively in a given subarray more than or equal to K times
- Generate an array of size K which satisfies the given conditions
- Count of N digit numbers possible which satisfy the given conditions
- Largest number with maximum trailing nines which is less than N and greater than N-D
- Count Numbers with N digits which consists of odd number of 0's
- Maximize number of nodes which are not part of any edge in a Graph
- Find i’th index character in a binary string obtained after n iterations | Set 2
- Count pieces of circle after N cuts
- Maximum sum after repeatedly dividing N by a divisor
- Find amount of water wasted after filling the tank
- Absolute difference between the first X and last X Digits of N
- Minimum number of operations to convert a given sequence into a Geometric Progression
- Program to print the berth of given railway seat number

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.