Maximize Matrix Binary Score

You are provided with a 2-D matrix called MAT consisting solely of 0s and 1s. Each row of the matrix can be viewed as a binary number, and the aggregate sum of these binary numbers from each row represents the score of the matrix.

Your task is to enhance this score by performing any number of operations. An operation consists of selecting any row or column and toggling every value in that row or column, meaning you switch all 0s to 1s and all 1s to 0s. Your goal is to determine the maximum possible score for the matrix MAT.

Input:

The first line contains an integer 'T', the number of test cases.
Each test case begins with two space-separated integers ‘M’ and ‘N’, denoting the matrix dimensions.
The following ‘M’ lines for each test case contain ‘N’ space-separated integers which represent the matrix, ‘MAT’.

Output:

For each test case, return the maximal possible score of the matrix ‘MAT’.
Provide the result for each test case on a separate line.

Example:

Input:
T = 1
M = 2, N = 3
MAT = [[0, 0, 1], [1, 1, 0]]
Output:
7
Explanation:

By toggling the first column, the matrix becomes [[1, 0, 1], [0, 1, 0]], and subsequently, by toggling the second row, it transforms to [[1, 0, 1], [1, 0, 1]]. These rows value as binary numbers (101 = 5, 101 = 5), leading to a total score of 7.

Constraints:

  • 1 ≤ T ≤ 5
  • 1 ≤ M, N ≤ 25
  • MAT[i][j] ∈ {0, 1}
  • Time limit: 1 second

Note:

Output handling is managed. Implement the function to return the correct results.
AnswerBot
1d

Given a binary matrix, maximize the score by toggling rows or columns to convert them to binary numbers and summing them up.

  • Iterate through each row and column to calculate the score of toggling that ...read more

Help your peers!
Add answer anonymously...
Goldman Sachs Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter