Set Matrix Zeroes - Java Solution

1. Introduction

This blog post explores the "Set Matrix Zeroes" problem, a common challenge in matrix manipulation. The task involves modifying an m x n integer matrix in such a way that if an element is 0, its entire row and column are set to 0's. The key requirement is to perform this operation in place, without using additional space for another matrix.

Problem

Given an m x n integer matrix, if any element is 0, set its entire row and column to 0's. The modification must be done in place.

2. Solution Steps

1. First pass: Iterate through the matrix to find zeroes, marking their rows and columns.

- Use the first row and first column of the matrix as markers.

- Have separate variables to keep track of whether the first row and first column need to be set to zeroes.

2. Second pass: Use the markers to set zeroes.

- Set elements to zero in rows and columns marked during the first pass, except for the first row and column.

3. Finally, if the first row or first column needs to be set to zero (based on the separate variables), do so.

4. This approach ensures that the space complexity is kept to O(1).

3. Code Program

public class SetMatrixZeroes {
    public static void main(String[] args) {
        int[][] matrix = {
            {1,1,1},
            {1,0,1},
            {1,1,1}
        };
        setZeroes(matrix);
        printMatrix(matrix);
    }

    // Function to set zeroes in the matrix
    public static void setZeroes(int[][] matrix) {
        boolean firstRowZero = false;
        boolean firstColZero = false;

        // Determine if the first row or first column is all zero
        for (int i = 0; i < matrix.length; i++) {
            if (matrix[i][0] == 0) {
                firstColZero = true;
                break;
            }
        }
        for (int j = 0; j < matrix[0].length; j++) {
            if (matrix[0][j] == 0) {
                firstRowZero = true;
                break;
            }
        }

        // Use first row and first column as markers
        for (int i = 1; i < matrix.length; i++) {
            for (int j = 1; j < matrix[0].length; j++) {
                if (matrix[i][j] == 0) {
                    matrix[i][0] = 0;
                    matrix[0][j] = 0;
                }
            }
        }

        // Set zeroes based on markers
        for (int i = 1; i < matrix.length; i++) {
            for (int j = 1; j < matrix[0].length; j++) {
                if (matrix[i][0] == 0 || matrix[0][j] == 0) {
                    matrix[i][j] = 0;
                }
            }
        }

        // Set zeroes for the first row and first column if needed
        if (firstColZero) {
            for (int i = 0; i < matrix.length; i++) {
                matrix[i][0] = 0;
            }
        }
        if (firstRowZero) {
            for (int j = 0; j < matrix[0].length; j++) {
                matrix[0][j] = 0;
            }
        }
    }

    // Helper function to print the matrix
    private static void printMatrix(int[][] matrix) {
        for (int[] row : matrix) {
            for (int val : row) {
                System.out.print(val + " ");
            }
            System.out.println();
        }
    }
}

Output:

0 0 0
0 0 0
0 0 0

Explanation:

1. setZeroes: This function sets zeroes in the matrix based on the presence of 0's in the original matrix.

2. It first checks if the first row or the first column contains any zeroes and keeps track of this information.

3. The function then uses the first row and first column as markers, storing a 0 in these places if a 0 is found in the respective row or column.

4. In the next step, it sets elements to zero in rows and columns based on these markers, excluding the first row and first column.

5. Finally, it sets the first row and first column to zeroes if needed, as indicated by the flags firstRowZero and firstColZero.

6. This method efficiently sets the required elements to zero without using extra space for marking, adhering to the in-place requirement.

7. printMatrix: This helper function prints the final state of the matrix after the zeroes are set.


Comments