algorithm - given 2-d array of 0/1, find min row to be deleted so that n can't be created by OR of any rows -
i came across interview question today, details follows:
you given 2 d array of 0's , 1's:
arr = [[0,1,0,1],\\binary rep of 5 [1,0,0,0],\\binary rep of 8 [0,1,1,1],\\binary rep of 7 [0,0,0,1]]\\binary rep of 1 given number n lets 9. if remove [1,0,0,0] wouldn't able create 9 rest of numbers. answer count = 1 , element(s) = [1,0,0,0]
the solution have is:
- create new 0/1 1-d array of size 4(length of each element in our case)
sum 1's if bit @ position 1, if bit @ position 0 we'll store "n - (1's count)". e.g. 9's binary representation [1,0,0,1] hence new array be:
[1(total 1's in first index), 2(total 0's in second), 3(total 0's in 3rd index), 3(total 1's in 4th)]here can remove element 1 present in first index. i.e. [1,0,0,0]
i have feeling approach wrong, , looking out modifications make right or pointers if approach entirely wrong.
you forgot step 0: discard every row has 1 n has 0. algorithm otherwise correct, because none of discarded rows can part of or equaling n, , or of subsets of remaining rows not n if , if or of not n, case if , if column 0 in or when should 1, accomplished if , if rows 1 in column deleted.
Comments
Post a Comment