


The first line of input contains an integer 'T' representing the number of test cases.
The first line of each test case contains two space-separated integers, ‘M’ and ‘N’, representing the number of rows and columns of the matrix, ‘MAT’, respectively.
The next ‘M’ lines of each test case contain ‘N’ space-separated integers representing the elements of the matrix, ‘MAT’.
For each test case, return the highest possible score of the matrix, ‘MAT’.
The output of each test case will be printed in a separate line.
1 <= T <= 5
1 <= M, N <= 25
MAT[ i ][ j ] ∈ {0, 1}
Time limit: 1 second
You do not need to print anything, it has already been taken care of. Just implement the given function.
The idea is to use the properties of binary numbers to implement the solution in a brute force manner.
We know that the maximum number of 1s in a binary number will help in increasing the overall sum if they are as left as possible. One important observation that arises from this fact is that the first bit(extreme left) should be made 1 to maximize the number as 4(100) > 3(011). So, if the first column in any row is 0 then we should toggle that row to obtain 1 in the first column.
Now, for a particular column, we should try maximizing the number of 1s since all 1s will be at the same bit position. Hence, if the number of 1s increases, it will consequently increase the total sum. So, if any column contains more 0s than 1s (or numbers of 1s are less than ‘M / 2’, i.e., half of the number of rows) then we should toggle that column.
We will do this for all columns except column 1, as by doing so, we end up destroying the property mentioned above for column 1.
Algorithm:
Description of ‘TOGGLE_ROW’ function
This function will take three parameters :
int TOGGLE_ROW(MAT, ROW_INDEX, N):
Description of ‘TOGGLE_COLUMN’ function
This function will take three parameters :
int TOGGLE_ROW(MAT, COLUMN_INDEX, M):
The idea is to use the greedy approach.
Suppose a row represents a binary number: 110101. This can be written as 100000 + 010000 + 000000 + 000100 + 000000 + 000001.
So, 110101(53) = 100000(32) + 010000(16) + 000000(0) + 000100(4) + 000000(0) + 000001(1).
It means that a binary number represented by a row can be interpreted as the sum of the decimal place values of 1s in all columns.
So, the total score of the matrix can also be calculated by summing “decimal place value of 1 in a column * the total number of 1s in that column” over all the columns.
As discussed in Approach 1, the first column should have all entries as 1. So, the score obtained from the first column would be ‘(1 << (N - 1)) * M’.
Now, starting from the first column, we will find the number of values that are the same(0 or 1) for each row, with the first column element and store it in the variable, ‘COUNT’.
It is because if they are both 0, then they would be automatically toggled to 1, if we toggle the complete row.
Otherwise, if they are 1, the row will not be toggled. Hence, effectively, the variable ‘COUNT’ stores the maximum number of 1s in a column.
From Approach 1, it is clear that we have to maximize the number of 1s and thus the ‘count’. Intuitively, if ‘COUNT’ is the number of 1s, then ‘M - COUNT’ is the number of 0s. In Approach 1, we were toggling the column if the number of 0s is more than the number of 1s. So, we can consider that the column will have a ‘max(COUNT, M - COUNT)’ number of 1s. This will result in the score of ‘(1 << (N - 1 - j)) * max(COUNT, M - COUNT)’, where ‘j’ is the column index.
We will sum all these values for all columns (2 to N) and add with the score of 1st column, which would be the final maximum score.
Algorithm: