Largest possible square submatrix with maximum AND value
Last Updated :
30 May, 2022
Given an integer matrix mat [ ][ ] dimensions, the task is to find the largest possible square matrix from the given matrix with maximum AND value.
AND value of a matrix is defined as the value obtained after performing bitwise AND operation on all elements of the matrix.
Examples:
Input: mat [ ][ ] = {{2, 3, 3}, {2, 3, 3}, {2, 2, 2}}
Output: 4
Explanation:
Given square submatrix has AND value 2.
The submatrix
{{3, 3}
{3, 3}}
of size 4 has maximum AND value 3. All other square submatrices of size 4 have AND value 2.
Input: mat [ ][ ] =
{{9, 9, 9, 8},
{9, 9, 9, 6},
{9, 9, 9, 3},
{2, 2, 2, 2}}
Output: 9
Explanation:
The submatrix of size 9
{{9, 9, 9},
{9, 9, 9},
{9, 9, 9}}
have maximum AND value 9.
Naive Approach:
Generate all square submatrices from the given matrix. Initialize a variable answer to store the maximum & value for submatrices and another variable count to store the number of elements in the submatrix. Print the maximum value of count corresponding to maximum AND value answer obtained from all square submatrices.
Efficient Approach:
Follow the steps below to optimize the above solution:
- To maximize the & value, we need to find a submatrix that consists only of the maximum element in the matrix. This is because the maximum possible AND value in the matrix is the maximum element present in the matrix.
- Find the maximum possible value present in the matrix.
- Use Dynamic programming approach to get maximum size submatrix filled by the maximum matrix element only.
- Create an auxiliary dp[][] such that dp[i][j] stores the largest possible square submatrix mat[i][j] can be a part of such that the AND value of that submatrix is equal to mat[i][j].
- The recurrence relation is as follows:
If mat[i][j] is equal to {mat[i-1][j], mat[i][j-1], mat[i-1][j-1]} then consider all the three values as a square submatrix and update DP[i][j] as:
DP[i][j] = min(DP[i-1][j], DP[i][j-1], DP[i-1][j-1]) + 1
Otherwise,
DP[i][j] = 1
The answer would be the maximum of all DP [i][j]
- Finally, iterate over the dp[][] matrix and find the largest dp[i][j] for every mat[i][j] equal to the maximum element in the array.
Below is implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int MAX_value(vector<vector< int > > arr)
{
int row = arr.size();
int col = arr[0].size();
int dp[row][col];
memset (dp, sizeof (dp), 0);
int i = 0, j = 0;
int c = arr[0][0], p = 0;
int d = row;
for (i = 0; i < d; i++) {
for (j = 0; j < d; j++) {
if (c < arr[i][j]) {
c = arr[i][j];
}
if (i == 0 || j == 0) {
dp[i][j] = 1;
}
else {
if (arr[i - 1][j - 1] == arr[i][j]
&& arr[i - 1][j] == arr[i][j]
&& arr[i][j - 1] == arr[i][j]) {
dp[i][j]
= min(dp[i - 1][j - 1],
min(dp[i - 1][j],
dp[i][j - 1]))
+ 1;
}
else {
dp[i][j] = 1;
}
}
}
}
for (i = 0; i < d; i++) {
for (j = 0; j < d; j++) {
if (arr[i][j] == c) {
if (p < dp[i][j]) {
p = dp[i][j];
}
}
}
}
return p * p;
}
int main()
{
vector<vector< int > > arr
= { { 9, 9, 3, 3, 4, 4 },
{ 9, 9, 7, 7, 7, 4 },
{ 1, 2, 7, 7, 7, 4 },
{ 4, 4, 7, 7, 7, 4 },
{ 5, 5, 1, 1, 2, 7 },
{ 2, 7, 1, 1, 4, 4 } };
cout << MAX_value(arr) << endl;
return 0;
}
|
Java
import java.util.*;
class GFG{
static int MAX_value( int [][]arr)
{
int row = arr.length;
int col = arr[ 0 ].length;
int [][]dp = new int [row][col];
int i = 0 , j = 0 ;
int c = arr[ 0 ][ 0 ], p = 0 ;
int d = row;
for (i = 0 ; i < d; i++)
{
for (j = 0 ; j < d; j++)
{
if (c < arr[i][j])
{
c = arr[i][j];
}
if (i == 0 || j == 0 )
{
dp[i][j] = 1 ;
}
else
{
if (arr[i - 1 ][j - 1 ] == arr[i][j] &&
arr[i - 1 ][j] == arr[i][j] &&
arr[i][j - 1 ] == arr[i][j])
{
dp[i][j] = Math.min(dp[i - 1 ][j - 1 ],
Math.min(dp[i - 1 ][j],
dp[i][j - 1 ])) + 1 ;
}
else
{
dp[i][j] = 1 ;
}
}
}
}
for (i = 0 ; i < d; i++)
{
for (j = 0 ; j < d; j++)
{
if (arr[i][j] == c)
{
if (p < dp[i][j])
{
p = dp[i][j];
}
}
}
}
return p * p;
}
public static void main(String[] args)
{
int [][]arr = { { 9 , 9 , 3 , 3 , 4 , 4 },
{ 9 , 9 , 7 , 7 , 7 , 4 },
{ 1 , 2 , 7 , 7 , 7 , 4 },
{ 4 , 4 , 7 , 7 , 7 , 4 },
{ 5 , 5 , 1 , 1 , 2 , 7 },
{ 2 , 7 , 1 , 1 , 4 , 4 } };
System.out.print(MAX_value(arr) + "\n" );
}
}
|
Python3
def MAX_value(arr):
row = len (arr)
col = len (arr)
dp = [[ 0 for i in range (col)]
for j in range (row)]
i, j = 0 , 0
c, p = arr[ 0 ][ 0 ], 0
d = row
for i in range (d):
for j in range (d):
if (c < arr[i][j]):
c = arr[i][j]
if (i = = 0 or j = = 0 ):
dp[i][j] = 1
else :
if (arr[i - 1 ][j - 1 ] = = arr[i][j] and
arr[i - 1 ][j] = = arr[i][j] and
arr[i][j - 1 ] = = arr[i][j]):
dp[i][j] = min (dp[i - 1 ][j - 1 ],
min (dp[i - 1 ][j],
dp[i][j - 1 ])) + 1
else :
dp[i][j] = 1
for i in range (d):
for j in range (d):
if (arr[i][j] = = c):
if (p < dp[i][j]):
p = dp[i][j]
return p * p
arr = [ [ 9 , 9 , 3 , 3 , 4 , 4 ],
[ 9 , 9 , 7 , 7 , 7 , 4 ],
[ 1 , 2 , 7 , 7 , 7 , 4 ],
[ 4 , 4 , 7 , 7 , 7 , 4 ],
[ 5 , 5 , 1 , 1 , 2 , 7 ],
[ 2 , 7 , 1 , 1 , 4 , 4 ]]
print (MAX_value(arr))
|
C#
using System;
class GFG{
static int MAX_value( int [,]arr)
{
int row = arr.GetLength(0);
int col = arr.GetLength(1);
int [,]dp = new int [row, col];
int i = 0, j = 0;
int c = arr[0, 0], p = 0;
int d = row;
for (i = 0; i < d; i++)
{
for (j = 0; j < d; j++)
{
if (c < arr[i, j])
{
c = arr[i, j];
}
if (i == 0 || j == 0)
{
dp[i, j] = 1;
}
else
{
if (arr[i - 1, j - 1] == arr[i, j] &&
arr[i - 1, j] == arr[i, j] &&
arr[i, j - 1] == arr[i, j])
{
dp[i, j] = Math.Min(dp[i - 1, j - 1],
Math.Min(dp[i - 1, j],
dp[i, j - 1])) + 1;
}
else
{
dp[i, j] = 1;
}
}
}
}
for (i = 0; i < d; i++)
{
for (j = 0; j < d; j++)
{
if (arr[i, j] == c)
{
if (p < dp[i, j])
{
p = dp[i, j];
}
}
}
}
return p * p;
}
public static void Main(String[] args)
{
int [,]arr = { { 9, 9, 3, 3, 4, 4 },
{ 9, 9, 7, 7, 7, 4 },
{ 1, 2, 7, 7, 7, 4 },
{ 4, 4, 7, 7, 7, 4 },
{ 5, 5, 1, 1, 2, 7 },
{ 2, 7, 1, 1, 4, 4 } };
Console.Write(MAX_value(arr) + "\n" );
}
}
|
Javascript
<script>
function MAX_value(arr) {
var row = arr.length;
var col = arr[0].length;
var dp = Array(row);
for ( var i = 0;i<row;i++)
dp[i] = Array(col).fill(0);
var i = 0, j = 0;
var c = arr[0][0], p = 0;
var d = row;
for (i = 0; i < d; i++) {
for (j = 0; j < d; j++) {
if (c < arr[i][j]) {
c = arr[i][j];
}
if (i == 0 || j == 0) {
dp[i][j] = 1;
} else {
if (arr[i - 1][j - 1] == arr[i][j] &&
arr[i - 1][j] == arr[i][j] &&
arr[i][j - 1] == arr[i][j]) {
dp[i][j] = Math.min(dp[i - 1][j - 1],
Math.min(dp[i - 1][j],
dp[i][j - 1])) + 1;
} else {
dp[i][j] = 1;
}
}
}
}
for (i = 0; i < d; i++) {
for (j = 0; j < d; j++) {
if (arr[i][j] == c) {
if (p < dp[i][j]) {
p = dp[i][j];
}
}
}
}
return p * p;
}
var arr = [ [ 9, 9, 3, 3, 4, 4 ],
[ 9, 9, 7, 7, 7, 4 ],
[ 1, 2, 7, 7, 7, 4 ],
[ 4, 4, 7, 7, 7, 4 ],
[ 5, 5, 1, 1, 2, 7 ],
[ 2, 7, 1, 1, 4, 4 ] ];
document.write(MAX_value(arr) + "\n" );
</script>
|
Time Complexity: O(N*N), as we are using nested loops to traverse N*N times.
Auxiliary Space: O(N*N), as we are using extra space for matrix.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...