
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
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
#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
- Related Articles
- Score After Flipping Matrix in C++
- Maximum decimal value path in a binary matrix in C++
- Maximum subarray sum by flipping signs of at most K array elements in C++
- Maximum segment value after putting k breakpoints in a number in C++
- Delete elements with frequency atmost K in Python
- Maximum number of dots after throwing a dice N times in C++
- Maximum size rectangle binary sub-matrix with all 1s in C++
- Find maximum path length in a binary matrix in Python
- Find row number of a binary matrix having maximum number of 1s in C++
- Maximum size rectangle binary sub-matrix with all 1s in C++ Program
- Find pair of rows in a binary matrix that has maximum bit difference in C++
- Maximum Score Words Formed by Letters in C++
- Count binary strings with k times appearing adjacent two set bits in C++
- Maximum array sum that can be obtained after exactly k changes in C++
- Program to find maximum score by splitting binary strings into two parts in Python
