VOOZH about

URL: https://www.geeksforgeeks.org/dsa/count-of-odd-sum-submatrix-with-odd-element-count-in-the-matrix/

⇱ Count of odd sum Submatrix with odd element count in the Matrix - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Count of odd sum Submatrix with odd element count in the Matrix

Last Updated : 11 Nov, 2023

Given a matrix mat[][] of size N x N, the task is to count the number of submatrices with the following properties:

  • The sum of all elements in the submatrix is odd.
  • The number of elements in the submatrix is odd.

Examples:

Input: mat[][] = {{1, 2, 3}, {7, 5, 9}, {6, 8, 10}}
Output: 8
Explanation: As here total 8 submatrix is available in given matrix in which no. of elements is odd and sum of elements is also odd list of that matrix are {{1}}, {{3}}, {{7}}, {{5}}, {{9}}, {{7, 5, 9}}, {{2}, {5}, {8}}, {{1, 2, 3}, {7, 5, 9}, {5, 9, 6}}

Input: mat[][] = {{15, 2, 31}, {17, 11, 12}, {16, 51, 10}, {13, 52, 18}}
Output: 14

Naive Approach: We can solve this problem using a brute force approach, where we consider all possible submatrices of the given matrix and check if they satisfy the given properties.

Below is the code for the above approach:


Output
Number of odd sum submatrices with odd number of elements: 8

Time Complexity: O(N6), as 6 nested for loop requires to make matrix as required
Auxiliary Space: O(1), no extra space used.

Efficient Approach: To solve the problem follow the below idea:

  • To optimize the solution, we can use a technique called prefix sums. We can precompute the sum of all elements in the matrix up to a particular row and column and store it in a separate matrix. This precomputation will allow us to calculate the sum of any submatrix in constant time. 
  • Once we have precomputed the prefix sums, we can iterate over all possible submatrices and check if they satisfy the above properties. To check if the sum of elements in a submatrix is odd, we can subtract the prefix sum of the bottom-right corner from the prefix sum of the top-left corner. If the difference is odd, the sum of the submatrix is odd.
  • To check if the number of elements in a submatrix is odd, we can count the number of rows and columns in the submatrix and check if their product is odd.

Below are the steps for the above approach:

  • Initialize a prefix sum matrix with 0, of the same size as the given matrix.
  • Precompute the prefix sums of all elements up to each row and column in the prefix sum matrix.
    • prefixSum[i][j] = prefixSum[i - 1][j] + prefixSum[i][j - 1] - prefixSum[i - 1][j - 1] + matrix[i - 1][j - 1].
  • Iterate over all possible submatrices in the given matrix.
  • For each submatrix, calculate the sum of its elements using the prefix sum matrix.
  • If the sum is odd, count the number of elements in the submatrix.
  • If the number of elements is odd, increment the counter.
  • Return the final count.

Below is the code for the above approach:


Output
Number of odd sum submatrices with odd number of elements: 8

Time Complexity: O(N4), as 4 nested for loop
Auxiliary Space: O(N2), extra space needed for prefix matrix.

Comment
Article Tags: