Jumping Kady (Intuit India)
Learn this problemProblem statement
Kady is very energetic guy and he is fond of jumping. He is standing on a two dimension plane of size m*n square units. Plane is partitioned into unit squares. So in total there are m*n squares. Kady has his favourite number 'X', so each time when he will jump he will take jump of 'X' units.
In short, plane can be considered as a 2D matrix. Kady is currently standing at position S(p,q) where p is p^th row of matrix and q is q^th column of matrix. Kady wants to go from his position S to new position R(u,v) by taking jumps of exactly X units each time.
Determine if kady can reach his destination or not. If he can reach, print the minimum number of jumps he need to take to go from S to R.
Note:
- Kady cannot go out of plane. If he do so then he will fall off the plane and dies.
- If Kady wants to take jump from point
AtoBthen jump is only feasible if Euclidean distance between these two points isX.
Function
minimumJumps(m: int, n: int, X: int, p: int, q: int, u: int, v: int) → int
Complete the function minimumJumps in the editor.
minimumJumps has the following parameters:
int m: the number of rows in the planeint n: the number of columns in the planeint X: Kady's favourite number, the jump distanceint p: the row number of Kady's starting positionint q: the column number of Kady's starting positionint u: the row number of Kady's destinationint v: the column number of Kady's destination
Returns
int: the minimum number of jumps required to reach the destination or -1 if it's not possible
Examples
Example 1
m = 6n = 5X = 5p = 1q = 2u = 2v = 5return = 2Kady starts at (1,2). He can jump first to (6,2), then to (2,5); both jumps have Euclidean distance 5.
Therefore, the minimum number of jumps required is 2.
Constraints
1 ≤ m, n ≤ 10001 ≤ X ≤ 10001 ≤ p, u ≤ m1 ≤ q, v ≤ n