Given a binary matrix mat[][] of dimension m*n (0-based indexing), in finding the selection of 1âs such that 1 is situated at a selected place the place the similar row and similar column donât have every other 1âs.
Examples:
Enter: mat[][] = [[0, 0, 1], [0, 1, 0], [1, 0, 0]]
Output: 3
Rationalization: All 1âs within the matrix fulfill the given situation.Enter: mat[][] = [[1, 0, 1], [0, 1, 0], [1, 0, 0]]
Output: 1
Rationalization: mat[1][1] fulfill the given situation.
Manner: The means is according to matrix traversal.
Underneath are the stairs for the above means:
- Initialize auxiliary array rows[] and cols[] to trace what number of 1âs are within the corresponding row or column.
- Initialize a variable say, ans to retailer the selection of 1âs that fulfill the given situation.
- Now traverse during the matrix mat[][] and take a look at if mat[i][j]==1 then increment rows[i]++(for row) and increment cols[j]++(for column).
- Run a loop from i = 0 to i < m and take a look at if row[i] == 1,
- Run a loop from j = 0 to j < n, and take a look at if the part on the present place is 1 in mat[][] if mat[i][j] == 1
- Take a look at if cols[j] == 1, increment ans, else wreck.
- Run a loop from j = 0 to j < n, and take a look at if the part on the present place is 1 in mat[][] if mat[i][j] == 1
- Go back ans.
Underneath is the code for the above means:
C++
// C++ code for the above means: #come with <bits/stdc++.h> the use of namespace std; int findNumberOfOnes(vector<vector<char> >& mat) { const int m = mat.dimension(); const int n = mat[0].dimension(); int ans = 0; // Auxiliary row and col for monitoring // what number of B at that corresponding // row and col vector<int> rows(m); vector<int> cols(n); // Traversing during the mat[][] // matrix. If mat[i][j] == B then // increment rows[i]++(for row) and // increment cols[j]++(for column). for (int i = 0; i < m; ++i) for (int j = 0; j < n; ++j) if (mat[i][j] == 1) { ++rows[i]; ++cols[j]; } // Traversing during the mat[][] // matrix once more and if // rows[i]==1 || cols[j]==1 // then ans++, else proceed. for (int i = 0; i < m; ++i) if (rows[i] == 1) for (int j = 0; j < n; ++j) // If the present positon // is 1 on this row then // wreck and seek // the following row if (mat[i][j] == 1) { if (cols[j] == 1) ++ans; wreck; } go back ans; } // Drivers code int major() { vector<vector<char> > mat = { { 0, 0, 1 }, { 0, 1, 0 }, { 1, 0, 0 } }; // Serve as Name cout << findNumberOfOnes(mat); go back 0; }
Java
/*package deal no matter // don't write package deal identify right here */ // A program for locating the lonely colour import java.io.*; magnificence GFG { public static int findLonelyColor(char[][] portray) { int m = portray.period, n = portray[0].period; // Auxiliary row and col for monitoring what number of B at // that corresponding row and col int[] rows = new int[m]; int[] cols = new int[n]; // Traversing during the portray matrix. If // portray[i][j]==B then increment rows[i]++(for // row) and increment cols[j]++(for column). for (int i = 0; i < m; ++i) { for (int j = 0; j < n; ++j) { if (portray[i][j] == 'B') { rows[i]++; cols[j]++; } } } int ans = 0; // Traversing during the portray matrix once more and // if rows[i]==1 || cols[j]==1 then ans++, else // proceed. for (int i = 0; i < m; ++i) { if (rows[i] == 1) { for (int j = 0; j < n; ++j) { // If the present positon is 'B' on this // row then wreck and seek the following // row if (portray[i][j] == 'B' && cols[j] == 1) { ans++; wreck; } } } } go back ans; } public static void major(String[] args) { char[][] portray = { { 'W', 'W', 'B' }, { 'W', 'B', 'W' }, { 'B', 'W', 'W' } }; int ans = findLonelyColor(portray); Machine.out.println(ans); } }
Python3
# A program for locating the lonely colour from typing import Listing def findLonelyColor(self, portray: Listing[List[str]]) -> int: # Auxiliary row and col for monitoring what number of B at that # corresponding row and col m, n = len(portray), len(portray[0]) rows, cols = [0] * m, [0] * n # Traversing during the portray matrix. # If portray[i][j]== B then increment rows[i]++(for row) # and increment cols[j]++(for column). for i in vary(m): for j in vary(n): if portray[i][j] == 'B': rows[i] += 1 cols[j] += 1 ans = 0 # Traversing during the portray matrix once more and # if rows[i]== 1 || cols[j]== 1 then ans++, else proceed for i in vary(m): if rows[i] == 1: for j in vary(n): # If the present positon is 'B' on this row # then wreck and seek the following row if portray[i][j] == 'B' and cols[j] == 1: ans += 1 wreck go back ans if __name__ == "__main__": portray = [['W', 'W', 'B'], ['W', 'B', 'W'], ['B', 'W', 'W']] print(findLonelyColor(__name__, portray))
Output :
3
Time Complexity: O(m*n + m*n), The place m and n are the sizes of rows and columns
Auxiliary Area: O(m + n), used auxiliary array rows and cols of dimension m and n respectively