I have a matrix which looks like this:
| 1 | 0 | 0 | 1 | 0 |
| 1 | 1 | 0 | 1 | 0 |
| 1 | 0 | 1 | 1 | 0 |
| 1 | 0 | 0 | 1 | 0 |
| 0 | 0 | 0 | 1 | 1 |
<
Let me take a very wild guess on what you are trying to do. Hint from the mention of:
O(n) algorithmWell, you can not do that in O(n) and I can prove that it is O(n^2) only.
But my wild guess is that you doing a classic celebrity identification problem, and that you misunderstood the problem.
A celebrity is person that is known by every other person, but doesn't know any [other people].
I celebrity identification problem, you are trying to find something like:
Find the number i where
a[i][x] = 1 for all x -> every one knows the celebrity
a[x][i] = 0 for all x != i -> the celebrity doesn't know anyone else
And indeed with this extra constrain on what you are trying to find, there is an O(n) solution.