Home » Matrix-Backtrack » Make Row Col 1

Make Row Col 1

// arrays starts with 0, so always be careful in for loop(so whn dealing with arrays always start loop with 0)
// note after multiplying again i have to call for loop,


class Matrix_MakeRowCol1 {

public static void setZeroes(int[][] matrix) {
 // Start typing your Java solution below
 // DO NOT write main() function
 boolean[] row = new boolean[matrix.length];
 boolean[] column = new boolean[matrix[0].length];

 for(int i = 0; i < matrix.length; i++){
 for(int j = 0 ; j < matrix[0].length; j++){
 if(matrix[i][j] == 0){
 row[i] = true; // i have set particular row and col to true, now can do anythg(by initilaizing) with it...
 column[j] = true;
 }
 }
 }

 for(int i = 0; i < matrix.length; i++){
 for(int j = 0; j < matrix[0].length; j++){
 if(row[i] || column[j]){
 matrix[i][j] =0;
 }
 }
 }
 }
public static void main(String args[]) {
int m[][] = {
{ 5, 2, 1, 3 },
{ 7, 1, 0, 3 },
{ 8, 6, 2, 3 },
 };
int i, j ;

for(i=0; i<3; i++) {
for(j=0; j<4; j++) {
System.out.print(m[i][j] + " ");
}
System.out.println();
}

System.out.println();

setZeroes(m);

for(i=0; i<3; i++) {
for(j=0; j<4; j++) {
System.out.print(m[i][j] + " ");
}
System.out.println();
}

}
}

&nbsp;

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: