#K38427. Minimum Flips to Achieve a Uniform Binary Matrix

    ID: 26196 Type: Default 1000ms 256MiB

Minimum Flips to Achieve a Uniform Binary Matrix

Minimum Flips to Achieve a Uniform Binary Matrix

Given an (n \times m) binary matrix, your task is to determine the minimum number of flips required to make all the elements in the matrix identical, i.e. either all 0's or all 1's. In one flip you can change a 0 to 1 or a 1 to 0.

The solution can be derived by counting the number of 1's and 0's in the matrix and then taking the minimum of the two counts. Formally, if (count_1) denotes the number of 1's and (count_0) denotes the number of 0's, the answer is given by:
(\min(count_1,; count_0)).

inputFormat

The input is read from standard input (stdin). The first line contains two integers (n) and (m), representing the number of rows and columns of the matrix respectively. This is followed by (n) lines, each containing (m) space-separated integers (each either 0 or 1) representing the rows of the matrix.

outputFormat

Output to standard output (stdout) a single integer: the minimum number of flips required to make the matrix uniform.## sample

2 3
0 1 0
1 0 1
3