https://app.codility.com/cert/view/certGWXW3D-3AU2DKY9RHQY5FWD/details/

Matrix A, consisting of N rows and M columns, is given, with each cell containing the value 0 or 1. Rows are numbered from 0 to N−1 (from top to bottom). Columns are numbered from 0 to M−1 (from left to right). The values inside the matrix can be changed: you can select as many columns as you want, and in the selected column(s), every value will be flipped (from 0 to 1, or from 1 to 0).

The goal is to obtain the maximum number of rows whose contents are all the same value (that is, we count rows with all 0s and rows with all 1s).

Write a function:

function solution(\$A);

that, given matrix A, returns the maximum number of rows containing all the same values that can be obtained after flipping the selected columns.

``````

// you can write to stdout for debugging purposes, e.g.
// print "this is a debug message\n";

function solution(\$A) {
\$res=array();

\$n=sizeof(\$A);
\$m=sizeof(\$A);

for(\$i=0; \$i< \$n; \$i++)
{
\$r0=array();
\$r1=array();
for(\$j=0; \$j< \$m; \$j++)
if(\$A[\$i][\$j])
\$r1[]=\$j;
else
\$r0[]=\$j;

if(\$r0)
\$r0=implode(",", \$r0);
else
\$r0="-";
if(isset(\$res[\$r0]))
\$res[\$r0]++;
else
\$res[\$r0]=1;

if(\$r1)
\$r1=implode(",", \$r1);
else
\$r1="-";
if(isset(\$res[\$r1]))
\$res[\$r1]++;
else
\$res[\$r1]=1;
}

\$max=1;
foreach(\$res as \$r)
if(\$r > \$max)
\$max=\$r;

return \$max;
}

``````