Problem · Math

Minimum Moves for Two Knights to Meet

Learn this problem
MediumAmazon logoAmazonFULLTIMEONSITE INTERVIEW
See Amazon hiring insights

Problem statement

Two knights start at coordinates first = [x1, y1] and second = [x2, y2] on an infinite chessboard. They take turns, with the first knight moving first. On a turn, the chosen knight must make one standard knight move: two squares along one axis and one square along the other.

Return the minimum total number of moves until the two knights occupy the same coordinate. If they already share a coordinate, return 0.

Function

minimumKnightMeetingMoves(first: int[], second: int[]) → int

Examples

Example 1

first = [0,0]second = [1,2]return = 1

The first knight can reach the second knight in one move.

Example 2

first = [0,0]second = [1,0]return = 3

The adjacent displacement is the exceptional three-move case.

Example 3

first = [7,-4]second = [7,-4]return = 0

The knights already meet.

Constraints

  • first.length == second.length == 2
  • -10^9 <= first[i], second[i] <= 10^9

More Amazon problems

drafts saved locally
public int minimumKnightMeetingMoves(int[] first, int[] second) {
    // Write your solution here.
}
first[0,0]
second[1,2]
expected1
checking account