I came across this question in an interview.
You have a nxn matrix containing 1s and 0s. You have to find the a row containing maximum number of 1s in most efficient way. I know it can be done in n^2 but is there any optimum solution for this ?
I came across this question in an interview. You have a nxn matrix containing
Share
The optimal solution is O(n^2). Assume that the matrix contains only zeros. You have to inspect every cell to be sure of this. This is O(n^2).
You could optimize the algorithm so that it stops searching if it finds a row that contains only 1s (this must be optimal) or if it has seen so many zeros in a row that it could not possibly beat the maximum seen so far. This could give a significant speed-up in some cases, but it would still be O(n^2) in the general case.