Maximum product of 4 adjacent elements in matrix
Last Updated :
10 Nov, 2022
Given a square matrix, find the maximum product of four adjacent elements of the matrix. The adjacent elements of the matrix can be top, down, left, right, diagonal, or anti-diagonal. The four or more numbers should be adjacent to each other.
Note: n should be greater than or equal to 4 i.e n >= 4
Examples :
Input : n = 4
{{6, 2, 3 4},
{5, 4, 3, 1},
{7, 4, 5, 6},
{8, 3, 1, 0}}
Output : 1680
Explanation:
Multiplication of 6 5 7 8 produces maximum result and all element are adjacent to each other in one direction
Input : n = 5
{{1, 2, 3, 4, 5},
{6, 7, 8, 9, 1},
{2, 3, 4, 5, 6},
{7, 8, 9, 1, 0},
{9, 6, 4, 2, 3}}
Output: 3024
Explanation: Multiplication of 6 7 8 9 produces maximum result and all elements are adjacent to each other in one direction.
Asked in: Tolexo
Approach:
- Group 4 elements that are adjacent to each other in each row and calculate their maximum result.
- Group 4 elements that are adjacent to each other in each column and calculate their maximum results.
- Group 4 elements that are adjacent to each other in diagonal and calculate their maximum results.
- Group 4 elements that are adjacent to each other in anti-diagonal and calculate their maximum results.
- Compare all calculated maximum results.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
const int n = 5;
int FindMaxProduct( int arr[][n], int n)
{
int max = 0, result;
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < n; j++)
{
if ((j - 3) >= 0)
{
result = arr[i][j] * arr[i][j - 1] *
arr[i][j - 2] * arr[i][j - 3];
if (max < result)
max = result;
}
if ((i - 3) >= 0)
{
result = arr[i][j] * arr[i - 1][j] *
arr[i - 2][j] * arr[i - 3][j];
if (max < result)
max = result;
}
if ((i - 3) >= 0 && (j - 3) >= 0)
{
result = arr[i][j] * arr[i - 1][j - 1] *
arr[i - 2][j - 2] * arr[i - 3][j - 3];
if (max < result)
max = result;
}
if ((i - 3) >= 0 && (j - 3) <= 0)
{
result = arr[i][j] * arr[i - 1][j + 1] *
arr[i - 2][j + 2] * arr[i - 3][j + 3];
if (max < result)
max = result;
}
}
}
return max;
}
int main()
{
int arr[][5] = {{1, 2, 3, 4, 5},
{6, 7, 8, 9, 1},
{2, 3, 4, 5, 6},
{7, 8, 9, 1, 0},
{9, 6, 4, 2, 3}};
cout << FindMaxProduct(arr, n);
return 0;
}
|
Java
class GFG {
static final int n = 5 ;
static int FindMaxProduct( int arr[][], int n)
{
int max = 0 , result;
for ( int i = 0 ; i < n; i++)
{
for ( int j = 0 ; j < n; j++)
{
if ((j - 3 ) >= 0 )
{
result = arr[i][j] * arr[i][j - 1 ]
* arr[i][j - 2 ]
* arr[i][j - 3 ];
if (max < result)
max = result;
}
if ((i - 3 ) >= 0 )
{
result = arr[i][j] * arr[i - 1 ][j]
* arr[i - 2 ][j]
* arr[i - 3 ][j];
if (max < result)
max = result;
}
if ((i - 3 ) >= 0 && (j - 3 ) >= 0 )
{
result = arr[i][j] * arr[i - 1 ][j - 1 ]
* arr[i - 2 ][j - 2 ]
* arr[i - 3 ][j - 3 ];
if (max < result)
max = result;
}
if ((i - 3 ) >= 0 && (j - 3 ) <= 0 )
{
result = arr[i][j] * arr[i - 1 ][j + 1 ]
* arr[i - 2 ][j + 2 ]
* arr[i - 3 ][j + 3 ];
if (max < result)
max = result;
}
}
}
return max;
}
public static void main(String[] args)
{
int arr[][] = { { 1 , 2 , 3 , 4 , 5 },
{ 6 , 7 , 8 , 9 , 1 },
{ 2 , 3 , 4 , 5 , 6 },
{ 7 , 8 , 9 , 1 , 0 },
{ 9 , 6 , 4 , 2 , 3 } };
System.out.print(FindMaxProduct(arr, n));
}
}
|
Python3
n = 5
def FindMaxProduct(arr, n):
max = 0
for i in range (n):
for j in range ( n):
if ((j - 3 ) > = 0 ):
result = (arr[i][j] * arr[i][j - 1 ] *
arr[i][j - 2 ] * arr[i][j - 3 ])
if ( max < result):
max = result
if ((i - 3 ) > = 0 ) :
result = (arr[i][j] * arr[i - 1 ][j] *
arr[i - 2 ][j] * arr[i - 3 ][j])
if ( max < result):
max = result
if ((i - 3 ) > = 0 and (j - 3 ) > = 0 ):
result = (arr[i][j] * arr[i - 1 ][j - 1 ] *
arr[i - 2 ][j - 2 ] * arr[i - 3 ][j - 3 ])
if ( max < result):
max = result
if ((i - 3 ) > = 0 and (j - 3 ) < = 0 ):
result = (arr[i][j] * arr[i - 1 ][j + 1 ] *
arr[i - 2 ][j + 2 ] * arr[i - 3 ][j + 3 ])
if ( max < result):
max = result
return max
if __name__ = = "__main__" :
arr = [[ 1 , 2 , 3 , 4 , 5 ],
[ 6 , 7 , 8 , 9 , 1 ],
[ 2 , 3 , 4 , 5 , 6 ],
[ 7 , 8 , 9 , 1 , 0 ],
[ 9 , 6 , 4 , 2 , 3 ]]
print (FindMaxProduct(arr, n))
|
C#
using System;
public class GFG {
static int n = 5;
static int FindMaxProduct( int [, ] arr, int n)
{
int max = 0, result;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
if ((j - 3) >= 0) {
result = arr[i, j] * arr[i, j - 1]
* arr[i, j - 2]
* arr[i, j - 3];
if (max < result)
max = result;
}
if ((i - 3) >= 0) {
result = arr[i, j] * arr[i - 1, j]
* arr[i - 2, j]
* arr[i - 3, j];
if (max < result)
max = result;
}
if ((i - 3) >= 0 && (j - 3) >= 0) {
result = arr[i, j] * arr[i - 1, j - 1]
* arr[i - 2, j - 2]
* arr[i - 3, j - 3];
if (max < result)
max = result;
}
if ((i - 3) >= 0 && (j - 3) <= 0) {
result = arr[i, j] * arr[i - 1, j + 1]
* arr[i - 2, j + 2]
* arr[i - 3, j + 3];
if (max < result)
max = result;
}
}
}
return max;
}
static public void Main()
{
int [, ] arr = { { 1, 2, 3, 4, 5 },
{ 6, 7, 8, 9, 1 },
{ 2, 3, 4, 5, 6 },
{ 7, 8, 9, 1, 0 },
{ 9, 6, 4, 2, 3 } };
Console.Write(FindMaxProduct(arr, n));
}
}
|
PHP
<?php
$n = 5;
function FindMaxProduct( $arr , $n )
{
$max = 0; $result ;
for ( $i = 0; $i < $n ; $i ++)
{
for ( $j = 0; $j < $n ; $j ++)
{
if (( $j - 3) >= 0)
{
$result = $arr [ $i ][ $j ] *
$arr [ $i ][ $j - 1] *
$arr [ $i ][ $j - 2] *
$arr [ $i ][ $j - 3];
if ( $max < $result )
$max = $result ;
}
if (( $i - 3) >= 0)
{
$result = $arr [ $i ][ $j ] *
$arr [ $i - 1][ $j ] *
$arr [ $i - 2][ $j ] *
$arr [ $i - 3][ $j ];
if ( $max < $result )
$max = $result ;
}
if (( $i - 3) >= 0 and ( $j - 3) >= 0)
{
$result = $arr [ $i ][ $j ] *
$arr [ $i - 1][ $j - 1] *
$arr [ $i - 2][ $j - 2] *
$arr [ $i - 3][ $j - 3];
if ( $max < $result )
$max = $result ;
}
if (( $i - 3) >= 0 and ( $j - 3) <= 0)
{
$result = $arr [ $i ][ $j ] *
$arr [ $i - 1][ $j + 1] *
$arr [ $i - 2][ $j + 2] *
$arr [ $i - 3][ $j + 3];
if ( $max < $result )
$max = $result ;
}
}
}
return $max ;
}
$arr = array ( array (1, 2, 3, 4, 5),
array (6, 7, 8, 9, 1),
array (2, 3, 4, 5, 6),
array (7, 8, 9, 1, 0),
array (9, 6, 4, 2, 3));
echo FindMaxProduct( $arr , $n );
?>
|
Javascript
<script>
let n = 5;
function FindMaxProduct(arr,n)
{
let max = 0, result;
for (let i = 0; i < n; i++)
{
for (let j = 0; j < n; j++)
{
if ((j - 3) >= 0)
{
result = arr[i][j] * arr[i][j - 1]
* arr[i][j - 2]
* arr[i][j - 3];
if (max < result)
max = result;
}
if ((i - 3) >= 0)
{
result = arr[i][j] * arr[i - 1][j]
* arr[i - 2][j]
* arr[i - 3][j];
if (max < result)
max = result;
}
if ((i - 3) >= 0 && (j - 3) >= 0)
{
result = arr[i][j] * arr[i - 1][j - 1]
* arr[i - 2][j - 2]
* arr[i - 3][j - 3];
if (max < result)
max = result;
}
if ((i - 3) >= 0 && (j - 3) <= 0)
{
result = arr[i][j] * arr[i - 1][j + 1]
* arr[i - 2][j + 2]
* arr[i - 3][j + 3];
if (max < result)
max = result;
}
}
}
return max;
}
let arr = [[ 1, 2, 3, 4, 5 ],
[ 6, 7, 8, 9, 1 ],
[ 2, 3, 4, 5, 6 ],
[ 7, 8, 9, 1, 0 ],
[ 9, 6, 4, 2, 3 ]];
document.write(FindMaxProduct(arr, n));
</script>
|
Time Complexity: O(n2)
Auxiliary Space: O(1), as no extra space is used
For row-wise adjacent elements, we can generalize the method using a sliding window.
Note: All elements in the matrix must be non-zero.
Another Approach:
- For each row, create a window of size k. Find the product of k adjacent element as window product (wp).
- Iterate through the row from k to (row size), by sliding window approach, find the maximum product. Note: (row size)>=k.
- Assign the maximum product to a global maximum product.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxPro( int a[6][5], int n, int m, int k)
{
int maxi(1), mp(1);
for ( int i = 0; i < n; ++i)
{
int wp(1);
for ( int l = 0; l < k; ++l)
{
wp *= a[i][l];
}
mp = wp;
for ( int j = k; j < m; ++j)
{
wp = wp * a[i][j] / a[i][j - k];
maxi = max(maxi,max(mp,wp));
}
}
return maxi;
}
int main()
{
int n = 6, m = 5, k = 4;
int a[6][5] = { { 1, 2, 3, 4, 5 },
{ 6, 7, 8, 9, 1 },
{ 2, 3, 4, 5, 6 },
{ 7, 8, 9, 1, 0 },
{ 9, 6, 4, 2, 3 },
{ 1, 1, 2, 1, 1 } };
cout << maxPro(a, n, m, k);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int maxPro( int [][] a,
int n, int m,
int k)
{
int maxi = 1 , mp = 1 ;
for ( int i = 0 ; i < n; ++i)
{
int wp = 1 ;
for ( int l = 0 ; l < k; ++l)
{
wp *= a[i][l];
}
mp = wp;
for ( int j = k; j < m; ++j)
{
wp = wp * a[i][j] / a[i][j - k];
maxi = Math.max(
maxi,
Math.max(mp, wp));
}
}
return maxi;
}
public static void main(String[] args)
{
int n = 6 , m = 5 , k = 4 ;
int [][] a = new int [][] {
{ 1 , 2 , 3 , 4 , 5 }, { 6 , 7 , 8 , 9 , 1 },
{ 2 , 3 , 4 , 5 , 6 }, { 7 , 8 , 9 , 1 , 0 },
{ 9 , 6 , 4 , 2 , 3 }, { 1 , 1 , 2 , 1 , 1 }
};
int maxpro = maxPro(a, n, m, k);
System.out.println(maxpro);
}
}
|
Python3
def maxPro(a,n,m,k):
maxi = 1
mp = 1
for i in range (n):
wp = 1
for l in range (k):
wp * = a[i][l]
mp = wp
for j in range (k,m):
wp = wp * a[i][j] / a[i][j - k]
maxi = max (
maxi,
max (mp, wp))
return maxi
n = 6
m = 5
k = 4
a = [[ 1 , 2 , 3 , 4 , 5 ], [ 6 , 7 , 8 , 9 , 1 ],
[ 2 , 3 , 4 , 5 , 6 ], [ 7 , 8 , 9 , 1 , 0 ],
[ 9 , 6 , 4 , 2 , 3 ], [ 1 , 1 , 2 , 1 , 1 ]]
maxpro = maxPro(a, n, m, k)
print (maxpro)
|
C#
using System;
class GFG{
public static int maxPro( int [,] a, int n,
int m, int k)
{
int maxi = 1, mp = 1;
for ( int i = 0; i < n; ++i)
{
int wp = 1;
for ( int l = 0; l < k; ++l)
{
wp *= a[i, l];
}
mp = wp;
for ( int j = k; j < m; ++j)
{
wp = wp * a[i, j] / a[i, j - k];
maxi = Math.Max(maxi,
Math.Max(mp, wp));
}
}
return maxi;
}
static public void Main()
{
int n = 6, m = 5, k = 4;
int [,] a = {{ 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 1 },
{ 2, 3, 4, 5, 6 }, { 7, 8, 9, 1, 0 },
{ 9, 6, 4, 2, 3 }, { 1, 1, 2, 1, 1 }};
int maxpro = maxPro(a, n, m, k);
Console.WriteLine(maxpro);
}
}
|
Javascript
<script>
function maxPro(a,n,m,k)
{
let maxi = 1, mp = 1;
for (let i = 0; i < n; ++i)
{
let wp = 1;
for (let l = 0; l < k; ++l)
{
wp *= a[i][l];
}
mp = wp;
for (let j = k; j < m; ++j)
{
wp = wp * a[i][j] / a[i][j - k];
maxi = Math.max(
maxi,
Math.max(mp, wp));
}
}
return maxi;
}
let n = 6, m = 5, k = 4;
let a=[[1, 2, 3, 4, 5 ], [ 6, 7, 8, 9, 1 ],
[ 2, 3, 4, 5, 6 ], [ 7, 8, 9, 1, 0 ],
[ 9, 6, 4, 2, 3 ], [ 1, 1, 2, 1, 1 ]]
let maxpro = maxPro(a, n, m, k);
document.write(maxpro);
</script>
|
Time Complexity: O(n2)
Auxiliary Space: O(1), as no extra space is used
Share your thoughts in the comments
Please Login to comment...