@pacific in your case a[0][0] might be wrong. So lets do this : a[0][0] = a[0][0 to n-1] & a[1 to n-1][0] // & of all elements in first row and first column
for( int i=1; i<n; i++) { for( int j=1;j<n;j++) { a[0][i] &= a[j][i]; // a[0][i] contains & of all elements of ith column a[i][0] &= a[i][j]; // a[i][0] contains & of all elements of ith row } } for( int i=1; i<n; i++) { for( int j=1;j<n;j++) { a[i][j] = a[i][0] & a[0][j] ; } } On Feb 28, 9:12 am, Mayur <mayurhem...@gmail.com> wrote: > If the diagonal elements of the matrix are all 0s, then you'd have to set > every element in the matrix to 0 (i.e. O(N^2) operations ). I don't think, > therefore, that we can do better than O(N^2). The best we can do is to > perhaps, make it output sensitive. > > On Sun, Feb 27, 2011 at 6:14 PM, pacific pacific <pacific4...@gmail.com>wrote: > > > > > > > > > 1. do & operation on all the values in each column and store it in the > > first row of each column > > 2. do & operation on all the values in each row and store it in the first > > column of each row. > > (when writing at a[0][0] do & operation with the value computed at 1.) > > 3. Now to find out the value at a[i][j] ,you need to do a[i][0] & a[0][j] > > > On Sun, Feb 27, 2011 at 12:03 PM, Rajnish <rajnish.i...@gmail.com> wrote: > > >> 1.) Traverse the whole matrix and replace each 0 value with -1. > >> 2.) Traverse the matrix again,all the 1 values are replaced with 0 in > >> the row and column of the index where a -1 value is found. > >> 3.) Set all -1 values to zero and we have the output array. > >> time complexity: O(n^2) > >> space complexity: O(1) > > >> On Feb 27, 2:29 am, gaurav gupta <1989.gau...@googlemail.com> wrote: > >> > A NxN binary matrix is given. If a row contains a 0 all element in the > >> > row will be set to 0 and if a column contains a 0 all element of the > >> > column will be set to 0. You have to do it in O(1) space. > > >> > example : > > >> > input array : > > >> > 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 > > >> > result array : > > >> > 0 0 0 0 0 > >> > 0 0 0 0 0 > >> > 0 0 1 1 0 > >> > 0 0 0 0 0 > >> > 0 0 1 1 0 > > >> > Thanks & Regards, > >> > Gaurav Gupta > > >> -- > >> You received this message because you are subscribed to the Google Groups > >> "Algorithm Geeks" group. > >> To post to this group, send email to algogeeks@googlegroups.com. > >> To unsubscribe from this group, send email to > >> algogeeks+unsubscr...@googlegroups.com. > >> For more options, visit this group at > >>http://groups.google.com/group/algogeeks?hl=en. > > > -- > > regards, > > chinna. > > > -- > > You received this message because you are subscribed to the Google Groups > > "Algorithm Geeks" group. > > To post to this group, send email to algogeeks@googlegroups.com. > > To unsubscribe from this group, send email to > > algogeeks+unsubscr...@googlegroups.com. > > For more options, visit this group at > >http://groups.google.com/group/algogeeks?hl=en. -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to algogeeks+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.