Count Paths With the Given XOR Value

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 10^9 + 7.

Example 1
 2  1  5
 7 10  0
12  6  4
Inputgrid = [[2,1,5],[7,10,0],[12,6,4]], k = 11
Output3
There are 3 valid paths whose XOR of all numbers on the path equals 11.
Example 2
1 3 3 3
0 3 3 2
3 0 1 1
Inputgrid = [[1,3,3,3],[0,3,3,2],[3,0,1,1]], k = 2
Output5
There are 5 valid paths whose XOR of all numbers on the path equals 2.

Constraints

  • 1 <= m == grid.length <= 300
  • 1 <= n == grid[r].length <= 300
  • 0 <= grid[r][c] < 16
  • 0 <= k < 16

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate