Minimum Knight Moves - Problem

In an infinite chess board with coordinates from -infinity to +infinity, you have a knight at square [0, 0].

A knight has 8 possible moves it can make, as illustrated below. Each move is two squares in a cardinal direction, then one square in an orthogonal direction.

Return the minimum number of steps needed to move the knight to the square [x, y]. It is guaranteed the answer exists.

The 8 possible knight moves are: [+2,+1], [+2,-1], [-2,+1], [-2,-1], [+1,+2], [+1,-2], [-1,+2], [-1,-2].

Input & Output

Example 1 — Basic Case
$ Input: x = 2, y = 1
Output: 1
💡 Note: Knight can reach (2,1) from (0,0) in 1 move: (0,0) → (2,1)
Example 2 — Multiple Steps
$ Input: x = 5, y = 5
Output: 4
💡 Note: One optimal path: (0,0) → (2,1) → (4,2) → (5,4) → (5,5) in 4 moves
Example 3 — Negative Coordinates
$ Input: x = -3, y = -3
Output: 2
💡 Note: Using symmetry, same as reaching (3,3): (0,0) → (2,1) → (3,3) in 2 moves

Constraints

  • |x| + |y| ≤ 300

Visualization

Tap to expand
Knight's Minimum Path ProblemInfinite Chess BoardStart (0,0)Target (2,1)Knight Move: (+2,+1)Answer: 1 stepKnight can reach (2,1) in exactly 1 move from origin
Understanding the Visualization
1
Input
Knight at (0,0), target at (2,1)
2
Process
Find shortest path using knight moves
3
Output
Minimum steps = 1
Key Takeaway
🎯 Key Insight: BFS guarantees the shortest path in unweighted graphs like chess knight moves
Asked in
Amazon 35 Google 28 Microsoft 22 Facebook 18
28.5K Views
Medium Frequency
~25 min Avg. Time
842 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen