- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find number of columns flips to get maximum number of equal Rows in Python?
Suppose we have a binary matrix, we can select any number of columns in the given matrix and flip every cell in that column. converting a cell means, inverting the cell value. We have to find the maximum number of rows that have all values equal after some number of flips. So if the matrix is like
0 | 0 | 0 |
0 | 0 | 1 |
1 | 1 | 0 |
The output will be 2. This is because after converting values in the first two columns, the last two rows have equal values.
To solve this, we will follow these steps:
x := matrix, m := number of rows and n := number of columns and r := 0
for each element i in x
c := 0
a := a list for all elements l in i, insert l XOR i
for each element j in x
if j = i or j = a, then increase c by 1
r := max of c and r
return r
Let us see the following implementation to get better understanding:
Example
class Solution(object): def solve(self, matrix): x = matrix m = len(matrix) n = len(matrix[0] ) r =0 for i in x: c=0 a=[l ^ 1 for l in i] for j in x: if j== i or j ==a: c+=1 r=max(c, r) return r ob = Solution() matrix = [[0,0,0], [0,0,1], [1,1,0]] print(ob.solve(matrix))
Input
[[0,0,0], [0,0,1], [1,1,0]]
Output
2
- Related Articles
- Flip Columns For Maximum Number of Equal Rows in Python
- How to get the maximum number of occupied rows and columns in a worksheet in Selenium with python?
- Program to find minimum number of flips required to have alternating values in Python
- Program to find maximum number of coins we can get using Python
- Python Program to print a specific number of rows with Maximum Sum
- A PT teacher wants to arrange maximum possible number of 6000 students in a Held such that the number of rows is equal to the number of columns. Find the number of rows if 71 were left out after arrangement.
- Program to find maximum number of eaten apples in Python
- Program to find maximum number of balanced groups of parentheses in Python
- Program to find maximum number of non-overlapping substrings in Python
- Program to find out the number of pairs of equal substrings in Python
- How to get the number of rows and columns of a JTable in Java Swing
- Program to find maximum difference of any number and its next smaller number in Python
- Program to find number of ways where square of number is equal to product of two numbers in Python
- Program to find maximum number of coins we can collect in Python
- Program to find maximum number of balls in a box using Python
