Even Number of Knight Moves Problem
Description
LeetCode Problem 3996.
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
Solution
Approach: Parity
Every knight move changes x by 1 and y by 2, or the other way round, so the sum x + y always changes by an odd amount. In other words, each move takes the knight to a square of the opposite color.
- After an even number of moves the knight stands on a square of the starting color.
- After an odd number of moves it stands on the opposite color.
On an 8 x 8 board a knight can reach every square, so a same-colored target is always reachable in an even number of moves. It is enough to compare the parity of x + y for start and target.
Complexity: time O(1), space O(1).
class Solution:
def canReach(self, start: List[int], target: List[int]) -> bool:
return (start[0] + start[1]) % 2 == (target[0] + target[1]) % 2