Count Paths With the Given XOR Value - Solution & Explanation
Problem Statement
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
XORof all the numbers on the path must be equal tok.
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 < 16
Approach Overview
Problem Overview: Given a matrix of integers, count how many paths from the top-left cell to the bottom-right cell produce a XOR value equal to k. You can only move right or down. Each path accumulates XOR from all visited cells.
Approach 1: Brute Force DFS Enumeration (Exponential Time)
Explore every possible path from (0,0) to (m-1,n-1) using recursion. At each step, move either right or down and maintain the running XOR of visited cells. When you reach the bottom-right cell, check if the accumulated XOR equals k. This approach performs a full traversal of the path space, which grows exponentially with grid size. Time complexity is O(2^(m+n)) and space complexity is O(m+n) due to recursion depth. It works only for very small matrices.
Approach 2: DFS with Memoization (DP on State) (O(m * n * X))
Instead of recomputing paths repeatedly, cache results based on the state (row, col, currentXor). From a cell, recursively explore the right and down neighbors while updating the XOR using nextXor = currentXor ^ grid[r][c]. If the same state appears again, return the stored result immediately. The key insight: once you reach a cell with a specific XOR value, the number of ways to finish the path will always be the same. Time complexity becomes O(m * n * X), where X is the number of possible XOR states. Space complexity is also O(m * n * X) for the memo table. This approach uses ideas from dynamic programming and bit manipulation.
Approach 3: Bottom-Up Dynamic Programming (State Transition DP) (O(m * n * X))
Define a DP state dp[r][c][x] representing the number of ways to reach cell (r,c) with XOR value x. Initialize the starting state using the value of grid[0][0]. For each cell, propagate counts to the right and down neighbors while updating the XOR with the destination cell value. This effectively builds the solution row by row across the matrix. The final answer is dp[m-1][n-1][k]. Time complexity remains O(m * n * X) and space complexity is O(m * n * X). Using rolling arrays can reduce memory usage.
Recommended for interviews: The dynamic programming approach with XOR state tracking. Interviewers expect you to recognize that brute force enumerates too many paths, then introduce a DP state that includes the current XOR. Showing the brute force idea demonstrates understanding of the path structure, while the optimized DP solution shows strong knowledge of array traversal and state-based dynamic programming.
Solutions for this problem are being prepared.
Try solving it yourselfDetailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force DFS Path Enumeration | O(2^(m+n)) | O(m+n) | Understanding the basic path exploration logic or solving very small grids |
| DFS with Memoization (Top-Down DP) | O(m * n * X) | O(m * n * X) | General case when overlapping subproblems appear during recursion |
| Bottom-Up Dynamic Programming | O(m * n * X) | O(m * n * X) | Most structured approach for interviews and production implementations |
Video Solution
3393. Count Paths With the Given XOR Value | DP on Grid • Aryan Mittal • 1,545 views views
Watch 6 more video solutions →Frequently Asked Questions
Is Count Paths With the Given XOR Value easy or hard?
Count Paths With the Given XOR Value Python/Java solution
How to solve Count Paths With the Given XOR Value in O(n)?
What is the best approach for Count Paths With the Given XOR Value?
Is Count Paths With the Given XOR Value asked at Google/Amazon/Meta?
What data structure is used in Count Paths With the Given XOR Value?
What is the time complexity of Count Paths With the Given XOR Value?
Ready to solve this problem?
Practice Count Paths With the Given XOR Value with our built-in code editor and test cases.
Practice on FleetCode