java – Make surrounding elements zero in a matrix, when you find a zero value

Please can you suggest a better / Optimized logic or code for the question. It has been asked that when you find a zero value in a matrix, make that element the sum of its upper, lower ,left, right value and make the upper, lower ,left, right elements zero. I assume to the matrix provided has at least 2 rows and 2 columns but if you can post code for any type of matrix , it would be of utmost help. I have fully commented the code for easy understanding, just copy and paste it in editor and tweak the values of the matrix variable in the main function of the program.


import java.util.ArrayList;

public class Main {
    static void MakeSurroundingElementsZero(int i,int j,int()()matrix){
        //when the element is not on the boundary of matrix.means it has
        // all surrounding elements
        if(i >0 && i < matrix.length-1 && j >0 && j < matrix(i).length-1){
             matrix(i)(j-1)=matrix(i)(j+1)=matrix(i-1)(j)=matrix(i+1)(j)=0;
        }
        //when it is in the first row
        else if(i ==0){
            // when it is not the first or last element in the first row
            if(j >0 && j < matrix(i).length-1){
                 matrix(i)(j+1)=matrix(i)(j-1)=matrix(i+1)(j)=0;
            }
            //when it is the first element of the first row
            else if(j ==0){
                 matrix(i)(j+1)=matrix(i+1)(j)=0;
            }
            // when it is the last element of the first row
            else{
                matrix(i)(j-1)=matrix(i+1)(j)=0;
            }
        }
        //when the element is present in the last row
        else{
            //element not the last or the first in the last row
            if (j >0 && j < matrix(i).length-1 ){
                matrix(i)(j+1)=matrix(i-1)(j)=matrix(i)(j-1)=0;
            }
            //first element in the last row
            else if (j==0){
                matrix(i-1)(j)=matrix(i)(j+1)=0;
            }
            //last element of the last row
            else{
                matrix(i)(j-1)=matrix(i-1)(j)=0;
            }
        }
    }
    static void addElements(int i,int j,int()()matrix){
        //when the element is not on the boundary of matrix.means it has
        // all surrounding elements
        if(i >0 && i < matrix.length-1 && j >0 && j < matrix(i).length-1){
            matrix(i)(j) = matrix(i)(j-1)+matrix(i)(j+1)+matrix(i-1)(j)+matrix(i+1)(j);
        }
        //when it is in the first row
        else if(i ==0){
            // when it is not the first or last element in the first row
            if(j >0 && j < matrix(i).length-1){
                matrix(i)(j) = matrix(i)(j+1)+matrix(i)(j-1)+matrix(i+1)(j);
            }
            //when it is the first element of the first row
            else if(j ==0){
                matrix(i)(j) = matrix(i)(j+1)+matrix(i+1)(j);
            }
            // when it is the last element of the first row
            else{
                matrix(i)(j) = matrix(i)(j-1)+matrix(i+1)(j);
            }
        }
        //when the element is present in the last row
        else{
            //element not the last or the first in the last row
            if (j >0 && j < matrix(i).length-1 ){
                matrix(i)(j) = matrix(i)(j+1)+matrix(i-1)(j)+matrix(i)(j-1);
            }
            //first element in the last row
            else if (j==0){
                matrix(i)(j)= matrix(i-1)(j)+matrix(i)(j+1);
            }
            //last element of the last row
            else{
                matrix(i)(j) = matrix(i)(j-1)+matrix(i-1)(j);
            }
        }
    }
    static void MakeZeroes(int()() matrix) {
        ArrayList<Integer()> zeroElements = new ArrayList<Integer()>();
        for (int i = 0;i < matrix.length;i++) {
            for(int j = 0;j < matrix(i).length;j++){
                //checking each element for zero value
                if(matrix(i)(j) == 0){
                    addElements(i,j,matrix);
                    //store it as we haven't made the surrounding elements zero yet

                    Integer() zeroelem = {i,j};
                    zeroElements.add(zeroelem);

                }
            }
        }
        //finally, make surrounding elements zero
        for (int i = 0;i<zeroElements.size();i++){

                MakeSurroundingElementsZero(zeroElements.get(i)(0),zeroElements.get(i)(1),matrix);

        }
    }
    public static void main(String() args) {
        int()() matrix = {{2,0,4,0},
                          {5,9,7,9},
                          {2,0,8,0}};
        MakeZeroes(matrix);
        for (int i =0;i < matrix.length;i++){
            for (int j = 0; j < matrix(i).length;j++){
                System.out.print(matrix(i)(j)+" ");
            }
            System.out.println();
        }


    }
}

My approach is to:-

  • Go to each element.
  • Check if it is zero or not.
  • If zero, then check where that element lies , whether totally inside the matrix or on the boundary.
  • Add accordingly the elements around that zero value element.
  • Store the index of that zero element in an ArrayList.
  • Loop the ArrayList to make the surrounding elements zero.