Question:

For a given matrix NxN having 0 or 1’s only. You have to find the count of rows and columns where atleast one 1 occurs.

e,g

0 0 0 0

1 0 0 1

1 0 0 1

1 1 0 1

Row count having 1 atleast once: 3

Col count having 1 atleast once: 3


Solution:

1) Initialize row_count = 0
2) For each row.
a) Do OR of all elements in the row.
b) If OR is 1 then increment the row_count

Similarly, we can count columns with at least one 1.

Time Complexity: O(n^2)
Extra Space: O(1)

Time complexity can be reduced for average case performance of O(n). Worst case will be O(n**2)

Hint: Check all the rows, but need not check all the columns.