@pacific: Good Algorithm
// This is the preface solution of this problem [:)]
// Time complexity: O(n^2)
// Space complexity: O(1)
public class BinaryMatrix
{
public static void main(String arg[])
{
int[][] matrix = new int[][]
{{1,0,1,1,0},{0,1,1,1,0},{1,1,1,1,1},
{1,0,1,1,1},{1,1,1,1,1}};
int temp = 0;
temp = matrix[0][0];
for(int i=1; i<matrix.length;i++)
{
matrix[0][0] &= matrix[0][i];
temp &= matrix[i][0];
}
for(int i=1;i<matrix.length;i++)
{
for(int j=1;j<matrix.length;j++)
{
matrix[0][i] &= matrix[j][i];
matrix[i][0] &= matrix[i][j];
}
}
for(int i=1;i<matrix.length;i++)
{
for(int j=1;j<matrix.length;j++)
{
matrix[i][j] = matrix[i][0] & matrix[0][j];
}
}
for(int i=1; i<matrix.length;i++)
{
if(matrix[0][0] == 0)
{
matrix[0][i] = 0;
}
if(temp == 0)
{
matrix[i][0] = 0;
}
}
matrix[0][0] &= temp;
for(int i=0;i<matrix.length;i++)
{
for(int j=0;j<matrix.length;j++)
{
System.out.print(matrix[i][j] + " ");
}
System.out.print("\n");
}
}
}
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.