Skip to main content

Count Paths With the Given XOR Value - Solution & Explanation

MediumArrayDynamic ProgrammingBit ManipulationMatrix4 min readAsked at: Microsoft
Practice this problem

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 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 <= 300
  • 1 <= n == grid[r].length <= 300
  • 0 <= grid[r][c] < 16
  • 0 <= 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 yourself

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force DFS Path EnumerationO(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 ProgrammingO(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?
The problem is classified as Medium because it combines grid traversal with XOR state tracking. The main challenge is recognizing that the path state must include the current XOR value, which leads to a 3D dynamic programming solution.
Count Paths With the Given XOR Value Python/Java solution
Most solutions implement dynamic programming that propagates XOR states while moving right and down in the grid. The same logic works in Python, Java, C++, and Go by maintaining a DP table or memoization cache and updating XOR values at each transition.
How to solve Count Paths With the Given XOR Value in O(n)?
The problem cannot be solved in pure O(n) time because the state depends on both grid position and XOR value. The best practical solution is O(m * n * X) using dynamic programming, where each cell maintains counts for all reachable XOR states.
What is the best approach for Count Paths With the Given XOR Value?
Dynamic programming with a XOR state is the most effective approach. Track the number of ways to reach each cell with a specific XOR value using a 3D DP state (row, column, xor). This reduces the exponential search space to O(m * n * X), where X is the number of possible XOR values.
Is Count Paths With the Given XOR Value asked at Google/Amazon/Meta?
Matrix dynamic programming and XOR state problems frequently appear in interviews at companies like Amazon, Google, and Meta. Variants involving path counting with constraints or bitwise operations are especially common in system-level algorithm rounds.
What data structure is used in Count Paths With the Given XOR Value?
The core data structure is a dynamic programming table that stores counts for each cell and XOR state. Implementations typically use a 3D array or hash map keyed by (row, column, xor). Bit manipulation is used to update XOR values efficiently.
What is the time complexity of Count Paths With the Given XOR Value?
The optimal dynamic programming solution runs in O(m * n * X) time. The algorithm processes every cell in the grid and tracks possible XOR values that can appear at that position. Space complexity is also O(m * n * X) for storing intermediate states.

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