Problem · Graph

Jumping Kady (Intuit India)

Learn this problem
MediumIntuit logoIntuitINTERNOA

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

  1. Kady cannot go out of plane. If he do so then he will fall off the plane and dies.
  2. If Kady wants to take jump from point A to B then jump is only feasible if Euclidean distance between these two points is X.

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:

  1. int m: the number of rows in the plane
  2. int n: the number of columns in the plane
  3. int X: Kady's favourite number, the jump distance
  4. int p: the row number of Kady's starting position
  5. int q: the column number of Kady's starting position
  6. int u: the row number of Kady's destination
  7. int 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 = 2

Kady 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 ≤ 1000
  • 1 ≤ X ≤ 1000
  • 1 ≤ p, u ≤ m
  • 1 ≤ q, v ≤ n

More Intuit problems

drafts saved locally
public int minimumJumps(int m, int n, int X, int p, int q, int u, int v) {
    // write your code here
}
m6
n5
X5
p1
q2
u2
v5
expected2
checking account