Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given a 2D integer array grid with size m x n. You are also given an integer k.
Your task is to calculate the number of paths you can take from the top-left cell (0, 0) to the bottom-right cell (m - 1, n - 1) satisfying the following constraints:
(i, j) you may move to the cell (i, j + 1) or to the cell (i + 1, j) if the target cell exists.XOR of all the numbers on the path must be equal to k.Return the total number of such paths.
Since the answer can be very large, return the result modulo 109 + 7.
Example 1:
Input: grid = [[2, 1, 5], [7, 10, 0], [12, 6, 4]], k = 11
Output: 3
Explanation:
The 3 paths are:
(0, 0) → (1, 0) → (2, 0) → (2, 1) → (2, 2)(0, 0) → (1, 0) → (1, 1) → (1, 2) → (2, 2)(0, 0) → (0, 1) → (1, 1) → (2, 1) → (2, 2)Example 2:
Input: grid = [[1, 3, 3, 3], [0, 3, 3, 2], [3, 0, 1, 1]], k = 2
Output: 5
Explanation:
The 5 paths are:
(0, 0) → (1, 0) → (2, 0) → (2, 1) → (2, 2) → (2, 3)(0, 0) → (1, 0) → (1, 1) → (2, 1) → (2, 2) → (2, 3)(0, 0) → (1, 0) → (1, 1) → (1, 2) → (1, 3) → (2, 3)(0, 0) → (0, 1) → (1, 1) → (1, 2) → (2, 2) → (2, 3)(0, 0) → (0, 1) → (0, 2) → (1, 2) → (2, 2) → (2, 3)Example 3:
Input: grid = [[1, 1, 1, 2], [3, 0, 3, 2], [3, 0, 2, 2]], k = 10
Output: 0
Constraints:
1 <= m == grid.length <= 3001 <= n == grid[r].length <= 3000 <= grid[r][c] < 160 <= k < 16Problem summary: You are given a 2D integer array grid with size m x n. You are also given an integer k. Your task is to calculate the number of paths you can take from the top-left cell (0, 0) to the bottom-right cell (m - 1, n - 1) satisfying the following constraints: You can either move to the right or down. Formally, from the cell (i, j) you may move to the cell (i, j + 1) or to the cell (i + 1, j) if the target cell exists. The XOR of all the numbers on the path must be equal to k. Return the total number of such paths. Since the answer can be very large, return the result modulo 109 + 7.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming · Bit Manipulation
[[2,1,5],[7,10,0],[12,6,4]] 11
[[1,3,3,3],[0,3,3,2],[3,0,1,1]] 2
[[1,1,1,2],[3,0,3,2],[3,0,2,2]] 10
count-pairs-with-xor-in-a-range)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3393: Count Paths With the Given XOR Value
class Solution {
public int countPathsWithXorValue(int[][] grid, int k) {
final int MAX = 15;
final int m = grid.length;
final int n = grid[0].length;
Integer[][][] mem = new Integer[m][n][MAX + 1];
return count(grid, 0, 0, 0, k, mem);
}
private static final int MOD = 1_000_000_007;
// Return the number of paths from (i, j) to (m - 1, n - 1) with XOR value
// `xors`.
private int count(int[][] grid, int i, int j, int xors, int k, Integer[][][] mem) {
if (i == grid.length || j == grid[0].length)
return 0;
xors ^= grid[i][j];
if (i == grid.length - 1 && j == grid[0].length - 1)
return xors == k ? 1 : 0;
if (mem[i][j][xors] != null)
return mem[i][j][xors];
final int right = count(grid, i, j + 1, xors, k, mem) % MOD;
final int down = count(grid, i + 1, j, xors, k, mem) % MOD;
return mem[i][j][xors] = (right + down) % MOD;
}
}
// Accepted solution for LeetCode #3393: Count Paths With the Given XOR Value
package main
// https://space.bilibili.com/206214
func countPathsWithXorValue(grid [][]int, k int) int {
const mod = 1_000_000_007
u := 0
for _, row := range grid {
for _, val := range row {
u |= val
}
}
if k > u {
return 0
}
m, n := len(grid), len(grid[0])
f := make([][][]int, m+1)
for i := range f {
f[i] = make([][]int, n+1)
for j := range f[i] {
f[i][j] = make([]int, u+1)
}
}
f[1][1][grid[0][0]] = 1
for i, row := range grid {
for j, val := range row {
for x := range u + 1 {
f[i+1][j+1][x] += (f[i+1][j][x^val] + f[i][j+1][x^val]) % mod
}
}
}
return f[m][n][k]
}
func countPathsWithXorValue2(grid [][]int, k int) int {
const mod = 1_000_000_007
u := 0
for _, row := range grid {
for _, val := range row {
u |= val
}
}
if k > u {
return 0
}
m, n := len(grid), len(grid[0])
memo := make([][][]int, m)
for i := range memo {
memo[i] = make([][]int, n)
for j := range memo[i] {
memo[i][j] = make([]int, u+1)
for x := range memo[i][j] {
memo[i][j][x] = -1
}
}
}
var dfs func(int, int, int) int
dfs = func(i, j, x int) int {
if i < 0 || j < 0 {
return 0
}
val := grid[i][j]
if i == 0 && j == 0 {
if x == val {
return 1
}
return 0
}
p := &memo[i][j][x]
if *p != -1 {
return *p
}
*p = (dfs(i, j-1, x^val) + dfs(i-1, j, x^val)) % mod
return *p
}
return dfs(m-1, n-1, k)
}
# Accepted solution for LeetCode #3393: Count Paths With the Given XOR Value
class Solution:
def countPathsWithXorValue(self, grid: list[list[int]], k: int) -> int:
MOD = 1_000_000_007
m = len(grid)
n = len(grid[0])
@functools.lru_cache(None)
def count(i: int, j: int, xors: int) -> int:
"""
Return the number of paths from (i, j) to (m - 1, n - 1) with XOR value
`xors`.
"""
if i == m or j == n:
return 0
xors ^= grid[i][j]
if i == m - 1 and j == n - 1:
return int(xors == k)
right = count(i, j + 1, xors)
down = count(i + 1, j, xors)
return (right + down) % MOD
return count(0, 0, 0)
// Accepted solution for LeetCode #3393: Count Paths With the Given XOR Value
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3393: Count Paths With the Given XOR Value
// class Solution {
// public int countPathsWithXorValue(int[][] grid, int k) {
// final int MAX = 15;
// final int m = grid.length;
// final int n = grid[0].length;
// Integer[][][] mem = new Integer[m][n][MAX + 1];
// return count(grid, 0, 0, 0, k, mem);
// }
//
// private static final int MOD = 1_000_000_007;
//
// // Return the number of paths from (i, j) to (m - 1, n - 1) with XOR value
// // `xors`.
// private int count(int[][] grid, int i, int j, int xors, int k, Integer[][][] mem) {
// if (i == grid.length || j == grid[0].length)
// return 0;
// xors ^= grid[i][j];
// if (i == grid.length - 1 && j == grid[0].length - 1)
// return xors == k ? 1 : 0;
// if (mem[i][j][xors] != null)
// return mem[i][j][xors];
// final int right = count(grid, i, j + 1, xors, k, mem) % MOD;
// final int down = count(grid, i + 1, j, xors, k, mem) % MOD;
// return mem[i][j][xors] = (right + down) % MOD;
// }
// }
// Accepted solution for LeetCode #3393: Count Paths With the Given XOR Value
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #3393: Count Paths With the Given XOR Value
// class Solution {
// public int countPathsWithXorValue(int[][] grid, int k) {
// final int MAX = 15;
// final int m = grid.length;
// final int n = grid[0].length;
// Integer[][][] mem = new Integer[m][n][MAX + 1];
// return count(grid, 0, 0, 0, k, mem);
// }
//
// private static final int MOD = 1_000_000_007;
//
// // Return the number of paths from (i, j) to (m - 1, n - 1) with XOR value
// // `xors`.
// private int count(int[][] grid, int i, int j, int xors, int k, Integer[][][] mem) {
// if (i == grid.length || j == grid[0].length)
// return 0;
// xors ^= grid[i][j];
// if (i == grid.length - 1 && j == grid[0].length - 1)
// return xors == k ? 1 : 0;
// if (mem[i][j][xors] != null)
// return mem[i][j][xors];
// final int right = count(grid, i, j + 1, xors, k, mem) % MOD;
// final int down = count(grid, i + 1, j, xors, k, mem) % MOD;
// return mem[i][j][xors] = (right + down) % MOD;
// }
// }
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.