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 <[email protected]>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 <[email protected]> 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 <[email protected]> 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 [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. >> >> > > > -- > 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 [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. > -- 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.
