Given a matrix of size M * N, the task is to find the count of the minimum number of cells that must be flipped such that there is at least a submatrix of size 2*2 with all equal elements.
Examples:
Input: mat[] = {“00000”, “10111”, “00000”, “11111”}
Output: 1
One of the possible submatrix could be {{0, 0}, {1, 0}}
where only a single element has to be flipped.Input: mat[] = {“0101”, “0101”, “0101”}
Output: 3
Approach: For every submatrix of size 2*2, count the number of 0s and the number of 1s in it and the minimum of these two will be the count of flips required to get the matrix with all equal elements. The minimum of this value for all the submatrices is the required answer.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the minimum flips // required such that the submatrix from // mat[i][j] to mat[i + 1][j + 1] // contains all equal elements int minFlipsSub(string mat[], int i, int j) { int cnt0 = 0, cnt1 = 0; if (mat[i][j] == '1') cnt1++; else cnt0++; if (mat[i][j + 1] == '1') cnt1++; else cnt0++; if (mat[i + 1][j] == '1') cnt1++; else cnt0++; if (mat[i + 1][j + 1] == '1') cnt1++; else cnt0++; return min(cnt0, cnt1); } // Function to return the minimum number // of slips required such that the matrix // contains at least a single submatrix // of size 2*2 with all equal elements int minFlips(string mat[], int r, int c) { // To store the result int res = INT_MAX; // For every submatrix of size 2*2 for (int i = 0; i < r - 1; i++) { for (int j = 0; j < c - 1; j++) { // Update the count of flips required // for the current submatrix res = min(res, minFlipsSub(mat, i, j)); } } return res; } // Driver code int main() { string mat[] = { "0101", "0101", "0101" }; int r = sizeof(mat) / sizeof(string); int c = mat[0].length(); cout << minFlips(mat, r, c); return 0; } |
Java
// Java implementation of the approach class GFG { // Function to return the minimum flips // required such that the submatrix from // mat[i][j] to mat[i + 1][j + 1] // contains all equal elements static int minFlipsSub(String mat[], int i, int j) { int cnt0 = 0, cnt1 = 0; if (mat[i].charAt(j) == '1') cnt1++; else cnt0++; if (mat[i].charAt(j+1) == '1') cnt1++; else cnt0++; if (mat[i + 1].charAt(j) == '1') cnt1++; else cnt0++; if (mat[i + 1].charAt(j+1) == '1') cnt1++; else cnt0++; return Math.min(cnt0, cnt1); } // Function to return the minimum number // of slips required such that the matrix // contains at least a single submatrix // of size 2*2 with all equal elements static int minFlips(String mat[], int r, int c) { // To store the result int res = Integer.MAX_VALUE; // For every submatrix of size 2*2 for (int i = 0; i < r - 1; i++) { for (int j = 0; j < c - 1; j++) { // Update the count of flips required // for the current submatrix res = Math.min(res, minFlipsSub(mat, i, j)); } } return res; } // Driver code public static void main(String[] args) { String mat[] = { "0101", "0101", "0101" }; int r = mat.length; int c = mat[0].length(); System.out.print(minFlips(mat, r, c)); } } // This code is contributed by 29AjayKumar |
Python 3
# Python 3 implementation of the approach import sys # Function to return the minimum flips # required such that the submatrix from # mat[i][j] to mat[i + 1][j + 1] # contains all equal elements def minFlipsSub(mat, i, j): cnt0 = 0 cnt1 = 0 if (mat[i][j] == '1'): cnt1 += 1 else: cnt0 += 1 if (mat[i][j + 1] == '1'): cnt1 += 1 else: cnt0 += 1 if (mat[i + 1][j] == '1'): cnt1 += 1 else: cnt0 += 1 if (mat[i + 1][j + 1] == '1'): cnt1 += 1 else: cnt0 += 1 return min(cnt0, cnt1) # Function to return the minimum number # of slips required such that the matrix # contains at least a single submatrix # of size 2*2 with all equal elements def minFlips(mat, r, c): # To store the result res = sys.maxsize # For every submatrix of size 2*2 for i in range(r - 1): for j in range(c - 1): # Update the count of flips required # for the current submatrix res = min(res, minFlipsSub(mat, i, j)) return res # Driver code if __name__ == '__main__': mat = ["0101", "0101", "0101"] r = len(mat) c = len(mat[0]) print(minFlips(mat, r, c)) # This code is contributed by Surendra_Gangwar |
C#
// C# implementation of the approach using System; class GFG { // Function to return the minimum flips // required such that the submatrix from // mat[i,j] to mat[i + 1,j + 1] // contains all equal elements static int minFlipsSub(String []mat, int i, int j) { int cnt0 = 0, cnt1 = 0; if (mat[i][j] == '1') cnt1++; else cnt0++; if (mat[i][j + 1] == '1') cnt1++; else cnt0++; if (mat[i + 1][j] == '1') cnt1++; else cnt0++; if (mat[i + 1][j + 1] == '1') cnt1++; else cnt0++; return Math.Min(cnt0, cnt1); } // Function to return the minimum number // of slips required such that the matrix // contains at least a single submatrix // of size 2*2 with all equal elements static int minFlips(String []mat, int r, int c) { // To store the result int res = int.MaxValue; // For every submatrix of size 2*2 for (int i = 0; i < r - 1; i++) { for (int j = 0; j < c - 1; j++) { // Update the count of flips required // for the current submatrix res = Math.Min(res, minFlipsSub(mat, i, j)); } } return res; } // Driver code public static void Main(String[] args) { String []mat = { "0101", "0101", "0101" }; int r = mat.Length; int c = mat.GetLength(0); Console.Write(minFlips(mat, r, c)); } } // This code is contributed by 29AjayKumar |
2
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Minimum Numbers of cells that are connected with the smallest path between 3 given cells
- Count of cells in a matrix which give a Fibonacci number when the count of adjacent cells is added
- Count of cells in a matrix whose adjacent cells's sum is prime Number
- Minimum cells required to reach destination with jumps equal to cell values
- Minimum sum submatrix in a given 2D array
- Check if matrix A can be converted to B by changing parity of corner elements of any submatrix
- Find Maximum Length Of A Square Submatrix Having Sum Of Elements At-Most K
- Check if the number is valid when flipped upside down
- Maximum number of 0s that can be flipped such that Array has no adjacent 1s
- Count of 0s to be flipped to make any two adjacent 1s at least K 0s apart
- Count of total bits toggled/flipped in binary representation of 0 to N
- Min number of moves to traverse entire Matrix through connected cells with equal values
- Minimum cells traversed to reach corner where every cell represents jumps
- Minimum Distance from a given Cell to all other Cells of a Matrix
- Submatrix Sum Queries
- Summed Area Table - Submatrix Summation
- Maximize the binary matrix by filpping submatrix once
- Maximum value in a matrix which contain intersecting concentric submatrix
- XOR of a submatrix queries
- Submatrix of given size with maximum 1's
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

