diff --git a/core-java-modules/core-java-arrays-operations-advanced-2/src/main/java/com/baeldung/matrixtozero/SetMatrixToZero.java b/core-java-modules/core-java-arrays-operations-advanced-2/src/main/java/com/baeldung/matrixtozero/SetMatrixToZero.java new file mode 100644 index 0000000000..57c558caa7 --- /dev/null +++ b/core-java-modules/core-java-arrays-operations-advanced-2/src/main/java/com/baeldung/matrixtozero/SetMatrixToZero.java @@ -0,0 +1,147 @@ +package com.baeldung.matrixtozero; + +import java.util.*; + +public class SetMatrixToZero{ + static void setZeroesByNaiveApproach(int[][] matrix) { + int row = matrix.length; + int col = matrix[0].length; + int [][] result = new int[row][col]; + + for(int i = 0; i rowSet = new HashSet<>(); + Set colSet = new HashSet<>(); + + for(int i = 0; i < rows; i++){ + for(int j = 0; j < cols; j++){ + if (matrix[i][j] == 0){ + rowSet.add(i); + colSet.add(j); + } + } + } + + for(int row: rowSet){ + for(int j = 0; j < cols; j++){ + matrix[row][j] = 0; + } + } + + for(int col: colSet) { + for(int i = 0; i < rows; i++){ + matrix[i][col] = 0; + } + } + } + + static boolean hasZeroInFirstRow(int[][] matrix, int cols) { + for (int j = 0; j < cols; j++) { + if (matrix[0][j] == 0) { + return true; + } + } + return false; + } + + static boolean hasZeroInFirstCol(int[][] matrix, int rows) { + for (int i = 0; i < rows; i++) { + if (matrix[i][0] == 0) { + return true; + } + } + return false; + } + + static void markZeroesInMatrix(int[][] matrix, int rows, int cols) { + for (int i = 1; i < rows; i++) { + for (int j = 1; j < cols; j++) { + if (matrix[i][j] == 0) { + matrix[i][0] = 0; + matrix[0][j] = 0; + } + } + } + } + + static void setZeroesInRows(int[][] matrix, int rows, int cols) { + for (int i = 1; i < rows; i++) { + if (matrix[i][0] == 0) { + for (int j = 1; j < cols; j++) { + matrix[i][j] = 0; + } + } + } + } + + static void setZeroesInCols(int[][] matrix, int rows, int cols) { + for (int j = 1; j < cols; j++) { + if (matrix[0][j] == 0) { + for (int i = 1; i < rows; i++) { + matrix[i][j] = 0; + } + } + } + } + + static void setZeroesInFirstRow(int[][] matrix, int cols) { + for (int j = 0; j < cols; j++) { + matrix[0][j] = 0; + } + } + + static void setZeroesInFirstCol(int[][] matrix, int rows) { + for (int i = 0; i < rows; i++) { + matrix[i][0] = 0; + } + } + + static void setZeroesByOptimalApproach(int[][] matrix) { + int rows = matrix.length; + int cols = matrix[0].length; + + boolean firstRowZero = hasZeroInFirstRow(matrix, cols); + boolean firstColZero = hasZeroInFirstCol(matrix, rows); + + markZeroesInMatrix(matrix, rows, cols); + + setZeroesInRows(matrix, rows, cols); + setZeroesInCols(matrix, rows, cols); + + if (firstRowZero) { + setZeroesInFirstRow(matrix, cols); + } + if (firstColZero) { + setZeroesInFirstCol(matrix, rows); + } + } + +} diff --git a/core-java-modules/core-java-arrays-operations-advanced-2/src/test/java/com/baeldung/matrixtozero/SetMatrixToZeroUnitTest.java b/core-java-modules/core-java-arrays-operations-advanced-2/src/test/java/com/baeldung/matrixtozero/SetMatrixToZeroUnitTest.java new file mode 100644 index 0000000000..ee4996457e --- /dev/null +++ b/core-java-modules/core-java-arrays-operations-advanced-2/src/test/java/com/baeldung/matrixtozero/SetMatrixToZeroUnitTest.java @@ -0,0 +1,54 @@ +package com.baeldung.matrixtozero; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import org.junit.jupiter.api.Test; + +public class SetMatrixToZeroUnitTest{ + @Test + void givenMatrix_whenUsingSetZeroesByNaiveApproach_thenSetZeroes() { + int[][] matrix = { + {1, 2, 3}, + {4, 0, 6}, + {7, 8, 9} + }; + int[][] expected = { + {1, 0, 3}, + {0, 0, 0}, + {7, 0, 9} + }; + SetMatrixToZero.setZeroesByNaiveApproach(matrix); + assertArrayEquals(expected, matrix); + } + + @Test + void givenMatrix_whenUsingSetZeroesByTimeOptimizedApproach_thenSetZeroes() { + int[][] matrix = { + {1, 2, 3}, + {4, 0, 6}, + {7, 8, 9} + }; + int[][] expected = { + {1, 0, 3}, + {0, 0, 0}, + {7, 0, 9} + }; + SetMatrixToZero.setZeroesByTimeOptimizedApproach(matrix); + assertArrayEquals(expected, matrix); + } + + @Test + void givenMatrix_whenUsingSetZeroesByOptimalApproach_thenSetZeroes() { + int[][] matrix = { + {1, 2, 3}, + {4, 0, 6}, + {7, 8, 9} + }; + int[][] expected = { + {1, 0, 3}, + {0, 0, 0}, + {7, 0, 9} + }; + SetMatrixToZero.setZeroesByOptimalApproach(matrix); + assertArrayEquals(expected, matrix); + } +}