# Maximum product of 4 adjacent elements in matrix

Given a square matrix, find the maximum product of four adjacent elements of matrix. The adjacent elements of 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.
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:
1. Group 4 elements which are adjacent to each other in each row and calculate their maximum result.
2. Group 4 elements which are adjacent to each other in each column and calculate their maximum results.
3. Group 4 elements which are adjacent to each other in diagonal and calculate their maximum results.
4. Group 4 elements which are adjacent to each other in anti diagonal and calculate their maximum results.
5. Compare of all calculated maximum results.

Below is the implementation of above approach:

## C++

 `// C++ program to find out the maximum product ` `// in the matrix which four elements are  ` `// adjacent to each other in one direction ` `#include ` `using` `namespace` `std; ` ` `  `const` `int` `n = 5; ` ` `  `// function to find max product ` `int` `FindMaxProduct(``int` `arr[][n], ``int` `n) ` `{ ` `    ``int` `max = 0, result; ` ` `  `    ``// iterate the rows. ` `    ``for` `(``int` `i = 0; i < n; i++)  ` `    ``{ ` ` `  `        ``// iterate the columns. ` `        ``for` `(``int` `j = 0; j < n; j++)  ` `        ``{ ` ` `  `            ``// check the maximum product  ` `            ``// in horizontal row. ` `            ``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; ` `            ``} ` ` `  `            ``// check the maximum product  ` `            ``// in vertical row. ` `            ``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; ` `            ``} ` ` `  `            ``// check the maximum product in ` `            ``// diagonal (going through down - right) ` `            ``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; ` `            ``} ` `             `  `            ``// check the maximum product in ` `            ``// diagonal (going through up - right) ` `            ``if` `((i - 3) >= 0 && (j - 1) <= 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; ` `} ` ` `  `// driver code ` `int` `main() ` `{ ` ` `  `    ``/* int arr[] = {{6, 2, 3, 4},  ` `                    ``{5, 4, 3, 1}, ` `                    ``{7, 4, 5, 6}, ` `                    ``{8, 3, 1, 0}};*/` `    ``/* int arr[] = {{1, 2, 1, 3, 4}, ` `                    ``{5, 6, 3, 9, 2}, ` `                    ``{7, 8, 8, 1, 2}, ` `                    ``{1, 0, 7, 9, 3}, ` `                    ``{3, 0, 8, 4, 9}};*/` `                         `  `    ``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}}; ` ` `  `    ``cout << FindMaxProduct(arr, n); ` `    ``return` `0; ` `} `

## Java

 `// Java program to find out the ` `// maximum product in the matrix ` `// which four elements are adjacent ` `// to each other in one direction ` `class` `GFG  ` `{ ` `static` `final` `int` `n = ``5``; ` ` `  `// function to find max product ` `static` `int` `FindMaxProduct(``int` `arr[][], ``int` `n)  ` `{ ` `    ``int` `max = ``0``, result; ` ` `  `    ``// iterate the rows. ` `    ``for` `(``int` `i = ``0``; i < n; i++)  ` `    ``{ ` `    ``// iterate the columns. ` `    ``for` `(``int` `j = ``0``; j < n; j++)  ` `    ``{ ` `        ``// check the maximum product ` `        ``// in horizontal row. ` `        ``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; ` `        ``} ` ` `  `        ``// check the maximum product ` `        ``// in vertical row. ` `        ``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; ` `        ``} ` ` `  `        ``// check the maximum product in ` `        ``// diagonal (going through down - right) ` `        ``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; ` `        ``} ` `  `  `        ``// check the maximum product in ` `        ``// diagonal (going through up - right) ` `        ``if` `((i - ``3``) >= ``0` `&& (j - ``1``) <= ``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; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args)  ` `{ ` ` `  `    ``/* int arr[] = {{6, 2, 3, 4}, ` `                       ``{5, 4, 3, 1}, ` `                       ``{7, 4, 5, 6}, ` `                       ``{8, 3, 1, 0}};*/` `    ``/* int arr[] = {{1, 2, 1, 3, 4}, ` `                       ``{5, 6, 3, 9, 2}, ` `                       ``{7, 8, 8, 1, 2}, ` `                       ``{1, 0, 7, 9, 3}, ` `                       ``{3, 0, 8, 4, 9}};*/` ` `  `    ``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)); ` `} ` `} ` ` `  `// This code is contributed by Anant Agarwal. `

## Python 3

 `# Python 3 program to find out the maximum  ` `# product in the matrix which four elements  ` `# are adjacent to each other in one direction ` `n ``=` `5` ` `  `# function to find max product ` `def` `FindMaxProduct(arr, n): ` ` `  `    ``max` `=` `0` ` `  `    ``# iterate the rows. ` `    ``for` `i ``in` `range``(n):  ` ` `  `        ``# iterate the columns. ` `        ``for` `j ``in` `range``( n):  ` ` `  `            ``# check the maximum product  ` `            ``# in horizontal row. ` `            ``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 ` ` `  `            ``# check the maximum product  ` `            ``# in vertical row. ` `            ``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 ` ` `  `            ``# check the maximum product in ` `            ``# diagonal going through down - right  ` `            ``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 ` ` `  `            ``# check the maximum product in ` `            ``# diagonal going through up - right ` `            ``if` `((i ``-` `3``) >``=` `0` `and` `(j ``-` `1``) <``=` `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` ` `  `# Driver code ` `if` `__name__ ``=``=` `"__main__"``: ` `     `  ` `  `    ``# int arr[] = {{6, 2, 3, 4},  ` `    ``#                  {5, 4, 3, 1}, ` `    ``#                  {7, 4, 5, 6}, ` `    ``#                  {8, 3, 1, 0}}; ` `    ``# int arr[] = {{1, 2, 1, 3, 4}, ` `    ``#                  {5, 6, 3, 9, 2}, ` `    ``#                  {7, 8, 8, 1, 2}, ` `    ``#                  {1, 0, 7, 9, 3}, ` `    ``#                  {3, 0, 8, 4, 9}}; ` `                         `  `    ``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)) ` ` `  `# This code is contributed by ita_c `

## C#

 `// C# program to find out the ` `// maximum product in the matrix ` `// which four elements are adjacent ` `// to each other in one direction ` `using` `System; ` ` `  `public` `class` `GFG { ` `     `  `    ``static` `int` `n = 5; ` ` `  `// Function to find max product ` `static` `int` `FindMaxProduct(``int``[,] arr, ``int` `n)  ` `{ ` `    ``int` `max = 0, result; ` ` `  `    ``// iterate the rows ` `    ``for` `(``int` `i = 0; i < n; i++) { ` `         `  `    ``// iterate the columns ` `    ``for` `(``int` `j = 0; j < n; j++) { ` `         `  `        ``// check the maximum product ` `        ``// in horizontal row. ` `        ``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; ` `        ``} ` ` `  `        ``// check the maximum product ` `        ``// in vertical row. ` `        ``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; ` `        ``} ` ` `  `        ``// check the maximum product in ` `        ``// diagonal going through down - right ` `        ``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; ` `        ``} ` `         `  `        ``// check the maximum product in  ` `        ``// diagonal going through up - right ` `        ``if` `((i - 3 ) >= 0 && (j - 1) <= 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; ` `} ` ` `  `    ``// Driver Code ` `    ``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)); ` `    ``} ` `} ` ` `  `// This code is contributed by Shrikant13 `

## PHP

 `= 0)  ` `            ``{ ` `                ``\$result` `= ``\$arr``[``\$i``][``\$j``] *  ` `                          ``\$arr``[``\$i``][``\$j` `- 1] * ` `                          ``\$arr``[``\$i``][``\$j` `- 2] *  ` `                          ``\$arr``[``\$i``][``\$j` `- 3]; ` `                 `  `                ``if` `(``\$max` `< ``\$result``) ` `                    ``\$max` `= ``\$result``; ` `            ``} ` ` `  `            ``// check the maximum product  ` `            ``// in vertical row. ` `            ``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``; ` `            ``} ` ` `  `            ``// check the maximum product in ` `            ``// diagonal going through down - right ` `            ``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``; ` `            ``} ` `             `  `            ``// check the maximum product in ` `            ``// diagonal going through up - right ` `            ``if` `((``\$i` `- 3) >= 0 ``and` `(``\$j` `- 1) <= 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``; ` `} ` `     `  `    ``// Driver Code                         ` `    ``\$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``); ` ` `  `// This code is contributed by anuj_67. ` `?> `

Output:

`3024`

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.