Overview
An m*n matrix is given. If an element is zero then set its row and column to zero
Examples
Input:
1, 1, 1
0, 1, 1
1, 1, 1
Output:
0, 1, 1
0, 0, 0
0, 1, 1
We will solve it by taking two additional arrays rowSet and columnSet of length m and n respectively. We will iterate through the matrix and
if matrix[i][j] == 0 then
rowSet[i] = 1
columnSet[j] = 1
We can then set matrix[i][j] to zero if rowSet[i] is equal to 1 or columenSet[j] is equal to 1
Program
Here is the program for the same.
package main
import "fmt"
func setZeroes(matrix [][]int) [][]int {
numRows := len(matrix)
numColumns := len(matrix[0])
rowSet := make([]int, numRows)
columnSet := make([]int, numColumns)
for i := 0; i < numRows; i++ {
for j := 0; j < numColumns; j++ {
if matrix[i][j] == 0 {
rowSet[i] = 1
columnSet[j] = 1
}
}
}
for i := 0; i < numRows; i++ {
for j := 0; j < numColumns; j++ {
if rowSet[i] == 1 || columnSet[j] == 1 {
matrix[i][j] = 0
}
}
}
return matrix
}
func main() {
matrix := [][]int{{1, 1, 1}, {0, 1, 1}, {1, 1, 1}}
output := setZeroes(matrix)
fmt.Println(output)
}
Output
[[0 1 1] [0 0 0] [0 1 1]]