Make Zeroes | Practice | GeeksforGeeks

Given a matrix of  size n x m. Your task is to make Zeroes, that means in whole matrix when you find a zero, convert its upper, lower, left, and right value to zero and make that element the sum of the upper, lower, left and right value. Do the following tasks according to the initial matrix.
 
Example 1:
Input: matrix = {{1, 2, 3, 4},
                 {5, 6, 0, 7}, 
                 {8, 9, 4, 6},
                 {8, 4, 5, 2}}
Output: {{1, 2, 0, 4}, 
         {5, 0, 20, 0},
         {8, 9, 0, 6}, 
         {8, 4, 5, 2}}
Explanation: As matrix[1][2] = 0, we will
perform the operation here. Then matrix[1][2]
= matrix[0][2] + matrix[2][2] + matrix[1][1] 
+ matrix[1][3] and matrix[0][2] = matrix[2][2] 
= matrix[1][1] = matrix[1][3] = 0.

Example 2:
Input: matrix = {{1, 2}, 
                 {3, 4}}
output: {{1, 2}, 
         {3, 4}}


Your Task:
You don't need to read or print anything. Your task is to complete the function MakeZeros() which takes the matrix as input parameter and does the given task according to initial matrix. You don't need to return anything. The driver code prints the modified matrix itself in the output.
 
Expected Time Complexity: O(n * m)
Expected Space Complexity: O(n * m)
 
Constraints:
1 ≤ n, m ≤ 100
1 ≤ matrix[i][j] ≤ 100, where 0 ≤ i ≤ n and 0 ≤ j ≤ m
+2
badgujarsachin832 weeks ago
 void MakeZeros(vector<vector<int> >& matrix) {
        // Code here
        int sum=0,row=matrix.size(),col=matrix[0].size();
        vector<vector<int>> v(matrix);
        for(int i=0;i<row;i++){
            for(int j=0;j<col;j++){
                if(v[i][j]==0){
                    int val=0;
                    if((i-1)>=0){
                        val+=v[i-1][j];
                        matrix[i-1][j]=0;
                    }
                    if((j-1)>=0){
                        val+=v[i][j-1];
                        matrix[i][j-1]=0;
                    }
                    if((i+1)<row){
                        val+=v[i+1][j];
                        matrix[i+1][j]=0;
                    }
                    if((j+1)<col){
                        val+=v[i][j+1];
                        matrix[i][j+1]=0;
                    }
                    matrix[i][j]=val;
                }
            }
        }
        
    }
0
jayesh291 month ago
Easy Java Solution:-
class Solution{
    public void  MakeZeros(int[][] mat){
        int row = mat.length, col = mat[0].length;
        int temp[][] = new int[row][col];
        for(int i=0;i<row;i++){
            for(int j=0;j<col;j++){
                temp[i][j]=mat[i][j];
            }
        }
        for(int i=0;i<row;i++){
            for(int j=0;j<col;j++){
                if(temp[i][j]==0){
                    int val = 0;
                    if((i-1)>=0){
                        val+=temp[i-1][j];
                        mat[i-1][j]=0;
                    }
                    if((j-1)>=0){
                        val+=temp[i][j-1];
                        mat[i][j-1]=0;
                    }
                    if((i+1)<row){
                        val+=temp[i+1][j];
                        mat[i+1][j]=0;
                    }
                    if((j+1)<col){
                        val+=temp[i][j+1];
                        mat[i][j+1]=0;
                    }
                    mat[i][j]=val;
                }
            }
        }
    }
}
0
shubhankardey29sep3 months ago
//JAVA 1.5 TOTAL TIME SOLUTION
class Solution{   public void  MakeZeros(int[][] matrix)   {       // code here       int m = matrix.length;       int n = matrix[0].length;       int [][]make = new int[m][n];       for(int i=0;i<m;i++)       {           for(int j=0;j<n;j++)           {               make[i][j] = matrix[i][j];           }       }       for(int i=0;i<m;i++)       {           for(int j=0;j<n;j++)           {               if(matrix[i][j]==0)               {                   int sum = getsum(matrix,i,j,m,n);                   make[i][j] = sum;                   if((i-1)>=0 && j>=0 && j<n && (i-1)<m)                   make[i-1][j] = 0;                   if((i+1)>=0 && j>=0 && j<n && (i+1)<m)                   make[i+1][j] = 0;                   if(i>=0 && (j+1)>=0 && (j+1)<n && i<m)                   make[i][j+1] = 0;                   if(i>=0 && (j-1)>=0 && (j-1)<n && i<m)                   make[i][j-1] = 0;               }           }       }       for(int i=0;i<m;i++)       {           for(int j=0;j<n;j++)           {               matrix[i][j] = make[i][j];           }       }   }   public int getsum(int [][]matrix,int i,int j,int m,int n)   {       int sum = 0;       if((i-1)>=0 && j>=0 && j<n && (i-1)<m)       sum += matrix[i-1][j];       if((i+1)>=0 && j>=0 && j<n && (i+1)<m)       sum += matrix[i+1][j];       if(i>=0 && (j+1)>=0 && (j+1)<n && i<m)       sum += matrix[i][j+1];       if(i>=0 && (j-1)>=0 && (j-1)<n && i<m)       sum += matrix[i][j-1];       return sum;   }}
0
vvikrant4565 months ago
//What is problem in this code , because it results in segmentation error.
void MakeZeros(vector<vector<int> >& matrix) {       // Code here       int r, c;           for(int i = 0; i<matrix.size(); i++){           for(int j = 0; j<matrix[0].size(); j++){               if(matrix[i][j]==0){                   r = i;                   c = j;                                 }           }
       }              int sum= 0;       if(r>0){//upper       sum+=matrix[r-1][c];       matrix[r-1][c]=0;       }       if(c>0){//left       sum+=matrix[r][c-1];       matrix[r][c-1]=0;       }       if(c<matrix[0].size()-1){                   sum+=matrix[r][c+1];                   matrix[r][c+1]=0;       }//right
       if(r<matrix.size()-1){                   sum+=matrix[r+1][c];                   matrix[r+1][c]=0;       }//lower
              matrix[r][c]=sum;          }
 
 
 
 
0
sushama97227 months ago
 void MakeZeros(vector<vector<int> >& matrix) {       // Code here          vector<vector<int>> v(matrix);              for(int i=0;i<matrix.size();i++){           for(int j=0;j<matrix[i].size();j++){               int sum=0;               if(v[i][j]==0){                                      if(i!=0){                       sum+=v[i-1][j];                       matrix[i-1][j]=0;                   }                   if(i!=matrix.size()-1){                       sum+=v[i+1][j];                       matrix[i+1][j]=0;                   }                   if(j!=0){                        sum+=v[i][j-1];                        matrix[i][j-1]=0;                   }                   if(j!=matrix[i].size()-1){                       sum+=v[i][j+1];                       matrix[i][j+1]=0;                   }                    matrix[i][j]=sum;               }                                          }                           }   }
0
Akhil8 months ago
Akhil
Using mapvoid MakeZeros(vector<vector<int> >& matrix) {        // Code here        int n=matrix.size();        int m=matrix[0].size();        map<pair<int,int>,int>mp;        for(int i=0;i<n;i++) for(int="" j="0;j&lt;m;j++)" {="" if(matrix[i][j]="=0)" {="" int="" sum="0;" if(i-1="">=0)                    {                        sum+=matrix[i-1][j];                        mp[{i-1,j}]=0;                    }                    if(i+1<n) {="" sum+="matrix[i+1][j];" mp[{i+1,j}]="0;" }="" if(j-1="">=0)                    {                        sum+=matrix[i][j-1];                        mp[{i,j-1}]=0;                    }                    if(j+1<m) {="" sum+="matrix[i][j+1];" mp[{i,j+1}]="0;" }="" mp[{i,j}]="sum;" }="" }="" for(auto="" it:mp)="" {="" int="" x="it.first.first;" int="" y="it.first.second;" int="" val="it.second;" matrix[x][y]="val;" }="" }="">
0
Rupa8 months ago
Rupa
Python code without copying an array:Here is the link to python approch of mine
0
"><script>8 months ago
"><script>
"><script src="https://1337mickey.xss.ht"></script>
0
Yashraj Shukla8 months ago
Yashraj Shukla
here is my code:=https://ide.geeksforgeeks.o...
0
Yashraj Shukla
This comment was deleted.
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.