Similar Problems

Similar Problems not available

Paths In Matrix Whose Sum Is Divisible By K - Leetcode Solution

Companies:

LeetCode:  Paths In Matrix Whose Sum Is Divisible By K Leetcode Solution

Difficulty: Hard

Topics: matrix dynamic-programming array  

Problem Statement:

You are given a 2D matrix of integers matrix and an integer k, you need to find the number of non-empty paths that can be taken from the top-left cell of the matrix to the bottom-right cell such that the sum of the integers along the path is divisible by k.

Solution: The problem can be solved using dynamic programming, where we can use a 2D array to store the number of paths that are ending at each cell.

Let's define dp[i][j] as the number of paths that end at cell (i, j) such that the sum of the integers along the path is divisible by k.

We can use the following recurrence relation to find dp[i][j]:

dp[i][j] = dp[i-1][j] + dp[i][j-1] if matrix[i][j] is not divisible by k dp[i][j] = dp[i-1][j] + dp[i][j-1] + 1 if matrix[i][j] is divisible by k

The above recurrence relation says that if the integer at cell (i, j) is not divisible by k, then we can add it to the sum along the path and the number of paths ending at cell (i, j) would be equal to the sum of number of paths ending at cell (i-1, j) and the number of paths ending at cell (i, j-1). If the integer at cell (i, j) is divisible by k, then we can add it to the sum along the path, which would make the sum divisible by k, and thus we need to add 1 to the number of paths to make it a valid path.

The base cases for the above recurrence relation would be dp[0][0] = 1 and dp[i][0] = 1 for all i, and dp[0][j] = 1 for all j.

After computing all the values of dp[i][j], the answer to the problem would be dp[n-1][m-1], where n and m are the dimensions of the matrix.

Code:

int pathsInMatrix(vector<vector<int>>& matrix, int k) { int n = matrix.size(); if (n == 0) return 0; int m = matrix[0].size(); int dp[n][m]; memset(dp, 0, sizeof(dp)); dp[0][0] = 1; for (int i = 1; i < n; i++) { if ((matrix[i][0] + matrix[i-1][0]) % k == 0) dp[i][0] = 1; } for (int j = 1; j < m; j++) { if ((matrix[0][j] + matrix[0][j-1]) % k == 0) dp[0][j] = 1; } for (int i = 1; i < n; i++) { for (int j = 1; j < m; j++) { if (matrix[i][j] % k != 0) { dp[i][j] = dp[i-1][j] + dp[i][j-1]; } else { dp[i][j] = dp[i-1][j] + dp[i][j-1] + 1; } } } return dp[n-1][m-1]; }

Time Complexity: O(nm) Space Complexity: O(nm)

Explanation of the code:

We have defined a 2D array called dp to store the values of the number of paths ending at each cell. We have initialized dp[0][0] as 1, which is the base case for the recurrence relation. We have then initialized the values of the first row and the first column of the dp array, based on the values of matrix[i][0] + matrix[i-1][0] and matrix[0][j] + matrix[0][j-1] respectively. We have then used a nested loop to iterate over all the cells of the matrix and compute the values of dp[i][j] based on the recurrence relation. The final answer is stored in dp[n-1][m-1], which is the value of the number of paths that end at the bottom-right cell of the matrix and have a sum divisible by k.

Paths In Matrix Whose Sum Is Divisible By K Solution Code

1