Minimum cells to be flipped to get a 2*2 submatrix with equal elements
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++
#include <bits/stdc++.h>
using namespace std;
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);
}
int minFlips(string mat[], int r, int c)
{
int res = INT_MAX;
for ( int i = 0; i < r - 1; i++) {
for ( int j = 0; j < c - 1; j++) {
res = min(res, minFlipsSub(mat, i, j));
}
}
return res;
}
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
class GFG
{
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);
}
static int minFlips(String mat[], int r, int c)
{
int res = Integer.MAX_VALUE;
for ( int i = 0 ; i < r - 1 ; i++)
{
for ( int j = 0 ; j < c - 1 ; j++)
{
res = Math.min(res, minFlipsSub(mat, i, j));
}
}
return res;
}
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));
}
}
|
Python 3
import sys
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)
def minFlips(mat, r, c):
res = sys.maxsize
for i in range (r - 1 ):
for j in range (c - 1 ):
res = min (res, minFlipsSub(mat, i, j))
return res
if __name__ = = '__main__' :
mat = [ "0101" , "0101" , "0101" ]
r = len (mat)
c = len (mat[ 0 ])
print (minFlips(mat, r, c))
|
C#
using System;
class GFG
{
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);
}
static int minFlips(String []mat,
int r, int c)
{
int res = int .MaxValue;
for ( int i = 0; i < r - 1; i++)
{
for ( int j = 0; j < c - 1; j++)
{
res = Math.Min(res, minFlipsSub(mat, i, j));
}
}
return res;
}
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));
}
}
|
Javascript
<script>
function minFlipsSub( mat , i , j)
{
var 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 minFlips(mat , r , c)
{
var res = Number.MAX_VALUE;
for (i = 0; i < r - 1; i++)
{
for (j = 0; j < c - 1; j++)
{
res = Math.min(res, minFlipsSub(mat, i, j));
}
}
return res;
}
var mat = [ "0101" , "0101" , "0101" ];
var r = mat.length;
var c = mat[0].length;
document.write(minFlips(mat, r, c));
</script>
|
Time Complexity: O(r * c)
Auxiliary Space: O(1)
Last Updated :
01 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...