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
4mo

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!
Select
Add answer anonymously...

Goldman Sachs Software Developer Intern interview questions & answers

A Software Developer Intern was asked 5mo agoQ. Write the code for Merge Sort.
A Software Developer Intern was asked Q. Rectangle Area Problem Statement You are provided with a list of rectangles, eac...read more
A Software Developer Intern was asked Q. First Non-Repeating Character Problem Statement You are given a string consistin...read more

Popular interview questions of Software Developer Intern

A Software Developer Intern was asked 6mo agoQ1. Write the code for Merge Sort.
A Software Developer Intern was asked Q2. Rectangle Area Problem Statement You are provided with a list of rectangles, eac...read more
A Software Developer Intern was asked Q3. First Non-Repeating Character Problem Statement You are given a string consistin...read more
Goldman Sachs Software Developer Intern Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

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

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits