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.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
You are given a 0-indexed m x n integer matrix grid and an integer k. You are currently at position (0, 0) and you want to reach position (m - 1, n - 1) moving only down or right.
Return the number of paths where the sum of the elements on the path is divisible by k. Since the answer may be very large, return it modulo 109 + 7.
Example 1:
Input: grid = [[5,2,4],[3,0,5],[0,7,2]], k = 3 Output: 2 Explanation: There are two paths where the sum of the elements on the path is divisible by k. The first path highlighted in red has a sum of 5 + 2 + 4 + 5 + 2 = 18 which is divisible by 3. The second path highlighted in blue has a sum of 5 + 3 + 0 + 5 + 2 = 15 which is divisible by 3.
Example 2:
Input: grid = [[0,0]], k = 5 Output: 1 Explanation: The path highlighted in red has a sum of 0 + 0 = 0 which is divisible by 5.
Example 3:
Input: grid = [[7,3,4,9],[2,3,6,2],[2,3,7,0]], k = 1 Output: 10 Explanation: Every integer is divisible by 1 so the sum of the elements on every possible path is divisible by k.
Constraints:
m == grid.lengthn == grid[i].length1 <= m, n <= 5 * 1041 <= m * n <= 5 * 1040 <= grid[i][j] <= 1001 <= k <= 50Problem summary: You are given a 0-indexed m x n integer matrix grid and an integer k. You are currently at position (0, 0) and you want to reach position (m - 1, n - 1) moving only down or right. Return the number of paths where the sum of the elements on the path is divisible by k. Since the answer may be very large, return it modulo 109 + 7.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
[[5,2,4],[3,0,5],[0,7,2]] 3
[[0,0]] 5
[[7,3,4,9],[2,3,6,2],[2,3,7,0]] 1
unique-paths)unique-paths-ii)minimum-path-sum)dungeon-game)cherry-pickup)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2435: Paths in Matrix Whose Sum Is Divisible by K
class Solution {
public int numberOfPaths(int[][] grid, int K) {
final int mod = (int) 1e9 + 7;
int m = grid.length, n = grid[0].length;
int[][][] f = new int[m][n][K];
f[0][0][grid[0][0] % K] = 1;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
for (int k = 0; k < K; ++k) {
int k0 = ((k - grid[i][j] % K) + K) % K;
if (i > 0) {
f[i][j][k] = (f[i][j][k] + f[i - 1][j][k0]) % mod;
}
if (j > 0) {
f[i][j][k] = (f[i][j][k] + f[i][j - 1][k0]) % mod;
}
}
}
}
return f[m - 1][n - 1][0];
}
}
// Accepted solution for LeetCode #2435: Paths in Matrix Whose Sum Is Divisible by K
func numberOfPaths(grid [][]int, K int) int {
const mod = 1e9 + 7
m, n := len(grid), len(grid[0])
f := make([][][]int, m)
for i := range f {
f[i] = make([][]int, n)
for j := range f[i] {
f[i][j] = make([]int, K)
}
}
f[0][0][grid[0][0]%K] = 1
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
for k := 0; k < K; k++ {
k0 := ((k - grid[i][j]%K) + K) % K
if i > 0 {
f[i][j][k] = (f[i][j][k] + f[i-1][j][k0]) % mod
}
if j > 0 {
f[i][j][k] = (f[i][j][k] + f[i][j-1][k0]) % mod
}
}
}
}
return f[m-1][n-1][0]
}
# Accepted solution for LeetCode #2435: Paths in Matrix Whose Sum Is Divisible by K
class Solution:
def numberOfPaths(self, grid: List[List[int]], K: int) -> int:
mod = 10**9 + 7
m, n = len(grid), len(grid[0])
f = [[[0] * K for _ in range(n)] for _ in range(m)]
f[0][0][grid[0][0] % K] = 1
for i in range(m):
for j in range(n):
for k in range(K):
k0 = ((k - grid[i][j] % K) + K) % K
if i:
f[i][j][k] += f[i - 1][j][k0]
if j:
f[i][j][k] += f[i][j - 1][k0]
f[i][j][k] %= mod
return f[m - 1][n - 1][0]
// Accepted solution for LeetCode #2435: Paths in Matrix Whose Sum Is Divisible by K
impl Solution {
pub fn number_of_paths(grid: Vec<Vec<i32>>, K: i32) -> i32 {
const MOD: i32 = 1_000_000_007;
let m = grid.len();
let n = grid[0].len();
let K = K as usize;
let mut f = vec![vec![vec![0; K]; n]; m];
f[0][0][grid[0][0] as usize % K] = 1;
for i in 0..m {
for j in 0..n {
for k in 0..K {
let k0 = ((k + K - grid[i][j] as usize % K) % K) as usize;
if i > 0 {
f[i][j][k] = (f[i][j][k] + f[i - 1][j][k0]) % MOD;
}
if j > 0 {
f[i][j][k] = (f[i][j][k] + f[i][j - 1][k0]) % MOD;
}
}
}
}
f[m - 1][n - 1][0]
}
}
// Accepted solution for LeetCode #2435: Paths in Matrix Whose Sum Is Divisible by K
function numberOfPaths(grid: number[][], K: number): number {
const mod = 1e9 + 7;
const m = grid.length;
const n = grid[0].length;
const f: number[][][] = Array.from({ length: m }, () =>
Array.from({ length: n }, () => Array(K).fill(0)),
);
f[0][0][grid[0][0] % K] = 1;
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
for (let k = 0; k < K; ++k) {
const k0 = (k - (grid[i][j] % K) + K) % K;
if (i > 0) {
f[i][j][k] = (f[i][j][k] + f[i - 1][j][k0]) % mod;
}
if (j > 0) {
f[i][j][k] = (f[i][j][k] + f[i][j - 1][k0]) % mod;
}
}
}
}
return f[m - 1][n - 1][0];
}
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.