# Set Matrix Zeroes Problem Solution

Set Matrix Zeroes Problem: Given an `m x n` integer matrix `matrix`, if an element is `0`, set its entire row and column to `0`‘s.

You must do it in place.

```Input: matrix = [[1,1,1],[1,0,1],[1,1,1]]
Output: [[1,0,1],[0,0,0],[1,0,1]]```

## Problem Solution In Python

``````N=len(matrix)
n=len(matrix[0])
iarr=set()
jarr=set()

for i in range(N):
for j in range(n):
if matrix[i][j]==0:
if i not in iarr:
if j not in jarr:

for val in iarr:
for j in range(n):
matrix[val][j]=0
for val in jarr:
for i in range(N):
matrix[i][val]=0
``````

## Problem Solution In Java

``````class Solution {
public void setZeroes(int[][] matrix) {
int fRow = 1, fCol = 1;

for(int i = 0; i < matrix.length; i++) {
for(int j = 0; j < matrix[0].length; j++) {
if(matrix[i][j] == 0) {
if (i == 0) {
fRow = 0;
}
if (j == 0) {
fCol = 0;
}
matrix[0][j] = 0;
matrix[i][0] = 0;
}
}
}

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;
}
}
}
if (fRow == 0) {
for (int j = 0; j < matrix[0].length; j++) {
matrix[0][j] = 0;
}
}
if (fCol == 0) {
for (int i = 0; i < matrix.length; i++) {
matrix[i][0] = 0;
}
}
}
}
``````

## Problem Solution In C++

``````void setZeroes(vector<vector<int>>& matrix) {
vector<pair<int,int>>a;
int m = matrix.size();
int n = matrix[0].size();
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(matrix[i][j]==0)
a.push_back(make_pair(i,j));
}
}

for(auto e: a)
{
for(int j=0;j<n;j++)
{
if(matrix[e.first][j] != 0)
matrix[e.first][j]=0;
}
for(int i=0;i<m;i++)
{
if(matrix[i][e.second] != 0)
matrix[i][e.second]=0;
}
}
}
``````

## Problem Solution In C

``````void setZeroes(int** matrix, int matrixSize, int* matrixColSize){

int auxmat[matrixSize][(*matrixColSize)]; //auxmat is an auxillary matrix
int i,j,k;
for(i=0;i<matrixSize;i++)
{
for(j=0;j<*(matrixColSize);j++)
{
auxmat[i][j]=matrix[i][j];
}
}
for(i=0;i<matrixSize;i++)
{
for(j=0;j<(*matrixColSize);j++)
{
if(matrix[i][j]==0)
{
for(k=0;k<matrixSize;k++)
{
auxmat[k][j]=0;
}

for(k=0;k<(*matrixColSize);k++)
{
auxmat[i][k]=0;
}
}
}
}
for(i=0;i<matrixSize;i++)
{
for(j=0;j<(*matrixColSize);j++)
{
matrix[i][j]=auxmat[i][j];
}
}
}
``````