Even Number of Knight Moves - Solution & Explanation
Problem Statement
You are given two integer arrays start and target, where each array is of the form [x, y] representing a cell on a standard 8 x 8 chessboard.
Return true if a knight can move from start to target in an even number of moves. Otherwise, return false.
Note: A valid knight move consists of moving two squares in one direction and one square perpendicular to it. The figure below illustrates all eight possible moves from a cell.

Example 1:
Input: start = [1,1], target = [2,2]
Output: true
Explanation:
One possible sequence of moves is (1, 1) -> (3, 2) -> (2, 4) -> (4, 3) -> (2, 2).
The knight reaches the target in 4 moves, which is even. Thus, the answer is true.
Example 2:
Input: start = [4,5], target = [6,6]
Output: false
Explanation:āāāāāāā
It is impossible to reach target = [6, 6] from start = [4, 5] in an even number of moves. Thus, the answer is false.
Constraints:
start.length == target.length == 20 <= start[i], target[i] <= 7
Approach Overview
Problem Overview: You need to determine whether a knight on a chessboard can reach a target position using an even number of moves. The core observation is that knight movement alternates square color parity on every move, which makes this problem more about mathematical properties than full path simulation.
Approach 1: Breadth-First Search (BFS) Simulation (Time: O(n), Space: O(n))
The direct solution is to run a BFS from the starting cell and explore all 8 knight moves level by level. Each BFS layer represents one move count, so when you first reach the target, you can check whether the depth is even. This approach works well for bounded boards or when the board dimensions are small enough to explore safely. Use a queue and a visited set to avoid revisiting positions. Problems involving shortest movement paths often use Breadth-First Search because BFS guarantees the minimum move count.
Approach 2: Chessboard Parity Observation (Time: O(1), Space: O(1))
The optimal approach relies on parity. A knight always changes square color after every move because its movement changes coordinates by an odd total amount. If the start and target squares have the same color parity, the knight needs an even number of moves. If the colors differ, the move count must be odd. Instead of exploring the board, compute (row + col) % 2 for both positions and compare them. This converts the problem into a simple arithmetic check using Math and board parity properties.
Approach 3: Recursive Backtracking (Time: O(8^k), Space: O(k))
A recursive solution tries all possible knight moves until reaching the target or exceeding a move limit. While this demonstrates the movement rules clearly, it becomes exponentially expensive because each position branches into up to eight new states. This approach is mainly useful for understanding traversal logic or for interview discussion before optimizing into BFS or parity analysis. Recursive traversal problems often overlap with Graph exploration patterns.
Recommended for interviews: Start with BFS to show you understand shortest-path traversal on grids and unweighted graphs. Then optimize using the parity observation. Interviewers usually expect the O(1) parity solution because it demonstrates pattern recognition and mathematical reasoning rather than brute-force exploration.
Solution
Each knight move has an offset of (\pm 1, \pm 2) or (\pm 2, \pm 1), so the change in the coordinate sum x + y is always odd. In other words, every move flips the color of the square (black/white distinguished by (x + y) bmod 2).
Therefore:
- After an even number of moves, the start and target have the same color;
- After an odd number of moves, the start and target have different colors.
On an 8 times 8 chessboard, a knight can reach any square, and any path to a same-color square must have even length. Hence, it suffices to check whether (x + y) bmod 2 is equal for the start and the target.
The time complexity is O(1), and the space complexity is O(1).
Code
Python
Java
C++
Go
TypeScript
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| BFS Simulation | O(n) | O(n) | When shortest path exploration is required on bounded boards |
| Parity Observation | O(1) | O(1) | Best general solution for checking even or odd move count |
| Recursive Backtracking | O(8^k) | O(k) | Useful for learning traversal logic or constrained search depth |
Video Solution
3996. Even Number of Knight Moves (Leetcode Easy) ⢠Programming Live with Larry ⢠339 views views
Watch 2 more video solutions āFrequently Asked Questions
Is Even Number of Knight Moves easy or hard?
Even Number of Knight Moves Python/Java solution
How to solve Even Number of Knight Moves in O(1)?
What is the best approach for Even Number of Knight Moves?
Is Even Number of Knight Moves asked at Google/Amazon/Meta?
What data structure is used in Even Number of Knight Moves?
What is the time complexity of Even Number of Knight Moves?
Ready to solve this problem?
Practice Even Number of Knight Moves with our built-in code editor and test cases.
Practice on FleetCodeProblem Info
Table of Contents
Practice this problem
Open in Editor