Knight On Chess Board

Knight movement on a chess board

Given any source point and destination point on a chess board, we need to find whether Knight can move to the destination or not.

Knight's movements on a chess board

The above figure details the movements for a knight ( 8 possibilities ). Note that a knight cannot go out of the board.

If yes, then what would be the minimum number of steps for the knight to move to the said point.
If knight can not move from the source point to the destination point, then return -1

Input:

N, M, x1, y1, x2, y2
where N and M are size of chess board
x1, y1  coordinates of source point
x2, y2  coordinates of destination point

Output:

return Minimum moves or -1

Example

Input : 8 8 1 1 8 8
Output :  6
NOTE: You only need to implement the given function. Do not read input, instead use the arguments to the function. Do not print the output, instead return values as specified. Still have a doubt? Checkout Sample Codes for more details.
Start solving Knight On Chess Board on Interview Code Editor
Sign Up
to access hints and editorial solutions for Knight On Chess Board
Asked In:
  • Goldman Sachs
  • Amazon
Click here to start solving coding interview questions