Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Program to find maximum sum by flipping each row elements in Python
Suppose we have a 2D binary matrix. For any row or column in the given matrix we can flip all the bits. If we can perform any number of these operations, and we treat each row as a binary number, we have to find the largest sum that can be made of these numbers.
So, if the input is like ?
| 0 | 1 | 0 |
| 0 | 0 | 1 |
then the output will be 11, as if we flip both rows we get 101 and 110, then the sum is 5 + 6 = 11.
Algorithm
To solve this, we will follow these steps ?
- For each row, ensure it starts with 1 (most significant bit) by flipping if needed
- For each column (except first), flip the column if it has more 0s than 1s
- Convert each row from binary to decimal and sum all values
Example
class Solution:
def solve(self, matrix):
# Step 1: Ensure each row starts with 1 (flip row if needed)
for r in matrix:
if r[0] == 0:
for i in range(len(r)):
r[i] = -r[i] + 1
# Step 2: For each column (except first), flip if more 0s than 1s
for j in range(1, len(matrix[0])):
cnt = 0
for i in range(len(matrix)):
cnt += 1 if matrix[i][j] else -1
if cnt < 0:
for i in range(len(matrix)):
matrix[i][j] = -matrix[i][j] + 1
# Step 3: Convert each row to decimal and sum
ans = 0
for r in matrix:
a = 0
for v in r:
a = 2 * a + v
ans += a
return ans
# Test the solution
ob = Solution()
matrix = [[0, 1, 0], [0, 0, 1]]
print("Original matrix:", matrix)
result = ob.solve(matrix)
print("Maximum sum:", result)
Original matrix: [[0, 1, 0], [0, 0, 1]] Maximum sum: 11
How It Works
The algorithm works in three phases ?
- Row optimization: Each row represents a binary number. To maximize its value, the most significant bit (leftmost) should be 1. If a row starts with 0, we flip the entire row.
- Column optimization: For remaining columns, we want as many 1s as possible since they contribute to the sum. If a column has more 0s than 1s, we flip it.
- Binary to decimal conversion: Convert each optimized row from binary to decimal and sum all values.
Step-by-Step Trace
# Original matrix: [[0, 1, 0], [0, 0, 1]]
# Step 1: Flip rows starting with 0
# Row 0: [0,1,0] ? [1,0,1] (flipped)
# Row 1: [0,0,1] ? [1,1,0] (flipped)
# Matrix becomes: [[1,0,1], [1,1,0]]
# Step 2: Check columns 1 and 2
# Column 1: [0,1] ? count = -1+1 = 0 (no flip needed)
# Column 2: [1,0] ? count = 1-1 = 0 (no flip needed)
# Final matrix: [[1,0,1], [1,1,0]]
# Step 3: Convert to decimal
# Row 0: [1,0,1] ? 1*4 + 0*2 + 1*1 = 5
# Row 1: [1,1,0] ? 1*4 + 1*2 + 0*1 = 6
# Total sum: 5 + 6 = 11
matrix = [[0, 1, 0], [0, 0, 1]]
print("Step-by-step transformation:")
print("Original:", matrix)
# Simulate the algorithm steps
for r in matrix:
if r[0] == 0:
for i in range(len(r)):
r[i] = 1 - r[i] # Flip bit
print("After row flips:", matrix)
print("Binary [1,0,1] = decimal", int('101', 2))
print("Binary [1,1,0] = decimal", int('110', 2))
print("Sum:", int('101', 2) + int('110', 2))
Step-by-step transformation: Original: [[0, 1, 0], [0, 0, 1]] After row flips: [[1, 0, 1], [1, 1, 0]] Binary [1,0,1] = decimal 5 Binary [1,1,0] = decimal 6 Sum: 11
Conclusion
The key insight is to ensure each row starts with 1 for maximum value, then optimize remaining columns by maximizing the number of 1s. This greedy approach guarantees the maximum possible sum when treating rows as binary numbers.
Advertisements
