# Maximum score after flipping a Binary Matrix atmost K times in C++

In this problem, we are given a 2-D array arr[] consisting of boolean values (i.e 0’s and 1’s) and an integer K. Our task is to create a program to find the Maximum score after flipping a Binary Matrix atmost K times in C++.

Problem Description − Here, for the 2-D array, and the k moves, we need to find the number that is created by the elements of the array. In each move, we will take a row or column and flip all the elements of the row or column. The choice will be done to keep in mind that we need to maximize the number created after K flips are made in a row of the matrix. And we need to return the sum of all numbers created in the row.

Let’s take an example to understand the problem,

## Input

arr[][] = {
{1, 0, 0},
{0, 1, 1},
{1, 0, 1}
}
K = 2

## Output

19

## Explanation

We have two flips,

First flip − we will flip 2nd-row elements. This will make the array,

{{1, 0, 0},
{1, 0, 0},
{1, 0, 1}}

Second flip − we will flip 2nd column elements. This will make the array,

{{1, 1, 0},
{1, 1, 0},
{1, 1, 1}}

The final number created at each row are 6, 6, 7.

Maximum sum = 19.

## Solution Approach

To solve the problem, we need to make the element of the first column 1 first. As 1 at ith column will contribute to 2i which will be the largest possible number(if one set bit is considered). So, the leftmost column needs to have the maximum number of 1’s (set bits ) to make the sum max. Then we will go for other elements of each row.

To check if I need to flip the row or column. We need to check other values in the column. i.e. all first elements of the rows. If the number of 0’s is greater than the 1’s. We will flip the column otherwise flip the row.

To solve the problem, we will use the map data structure.

Program to illustrate the working of our solution,

## Example

Live Demo

#include <bits/stdc++.h>
using namespace std;
const int row = 3;
const int col = 3;
int MaxSumAfterFlip(int mat[row][col], int K) {
map<int, int> flipValues;
int updateVal, MaxSum = 0;
for (int i = 0; i < row; ++i) {
if (mat[i][0] == 0) {
updateVal = 0;
for (int j = 1; j < col; ++j)
updateVal = updateVal + mat[i][j] * pow(2, col - j- 1);
flipValues[updateVal] = i;
}
}
map<int, int>::iterator it = flipValues.begin();
while (K > 0 && it != flipValues.end()) {
int updateIndex = it->second ;
for (int j = 0; j < col; ++j)
mat[updateIndex][j] = (mat[updateIndex][j] + 1) % 2;
it++;
K--;
}
MaxSum = 0;
int zeros, ones = 0;
for (int j = 0; j < col; ++j) {
zeros = ones = 0;
for (int i = 0; i < row; ++i) {
mat[i][j] == 0 ? zeros++ : ones++;
}
if (K > 0 && zeros > ones) {
MaxSum += zeros * pow(2, (col - j - 1));
K--;
}
else
MaxSum += ones * pow(2, (col - j - 1));
}
return MaxSum;
}
int main() {
int mat[row][col] = {{1, 0, 0 },{0, 1, 1},{1, 0, 1}};
int K = 2;
cout<<"The Maximum score after flipping the matrix atmost K times is "<<MaxSumAfterFlip(mat, K);
return 0;
}

## Output

The Maximum score after flipping the matrix atmost K times is 19