algoblazerEarly Access
LearnProblemsLeaderboard
Log inSign up

All problems

‹ Back to map
7 shown · 0/261 solved

Two Knights Meeting Moves

SilverCommunity Beta
Asked atAmazon
Solve problem →
2000ms256MBAdded Sep 10, 2026

You are given two knights on an infinite chessboard, standing at integer coordinates (x1, y1) and (x2, y2). A knight move changes one coordinate by 2 and the other by 1, in any of the 8 combinations of signs and axes; the board has no edges, so every move is always available.

In each move, either knight makes one knight move. The knights may move in any order, and one knight may stay put for as long as you like. Return the minimum total number of moves until both knights occupy the same square.

Examples

Example 1

Input: x1 = 0, y1 = 0, x2 = 1, y2 = 2
Output: 1
Explanation: The first knight moves directly onto the second knight's square.

Example 2

Input: x1 = 0, y1 = 0, x2 = 1, y2 = 0
Output: 3
Explanation: Adjacent squares are three knight moves apart, e.g. (0,0) -> (2,1) -> (0,2) -> (1,0). Splitting the moves between the two knights cannot do better.

Example 3

Input: x1 = 5, y1 = -7, x2 = 5, y2 = -7
Output: 0
Explanation: The knights already share a square.

Constraints

  • −300≤-300 \leq−300≤ x1, y1, x2, y2 ≤300\leq 300≤300
  • The board is infinite: intermediate squares may lie anywhere.

Details

Solved by3 people
Time limit2000ms
Memory256MB
AddedSep 10, 20262 days ago