# Maximum neighbor element in a matrix within distance K

Given a matrix mat[][] and an integer K, the task is to find the maximum neighbor within an absolute distance of K for each element of the matrix.

In other words for each Matrix[i][j] find maximum Matrix[p][q] such that abs (i-p) + abs (j-q) ≤ K.

Examples:

Input: mat[][] = {{1, 2}, {4, 5}}, K = 1
Output: {{4, 5}, {5, 5}}
Explanation:
Maximum neighbour to the element mat = 4 (Distance = 1)
Maximum neighbour to the element mat = 5 (Distance = 1)
Maximum neighbour to the element mat = 5 (Distance = 1)
Maximum neighbour to the element mat = 5 (Distance = 0)

Input: mat[][] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, K = 2
Output: {{7, 8, 9}, {8, 9, 9}, {9, 9, 9}}

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

Approach: The idea is to iterate over a loop from 1 to K, to choose the element from neighbors with distance less than or equal to K. Each time, Iterate over the matrix to find the maximum adjacent element for each element of the matrix.

Below is the implementation of the above approach:

## C++

 `// C++ implementation to find the  ` `// maximum neighbor element within ` `// the distance of less than K ` ` `  `#include ` ` `  `using` `namespace` `std; ` ` `  `// Function to print the matrix ` `void` `printMatrix(vector > A) ` `{ ` `    ``// Loop to iterate over the matrix ` `    ``for` `(``int` `i = 0; i < A.size(); i++) { ` `        ``for` `(``int` `j = 0; j < A.size(); j++) ` `            ``cout << A[i][j] << ``' '``; ` `        ``cout << ``'\n'``; ` `    ``} ` `} ` ` `  `// Function to find the maximum ` `// neighbor within the distance ` `// of less than equal to K ` `vector > getMaxNeighbour( ` `        ``vector >& A, ``int` `K) ` `{ ` `    ``vector > ans = A; ` `     `  `    ``// Loop to find the maximum ` `    ``// element within the distance ` `    ``// of less than K ` `    ``for` `(``int` `q = 1; q <= K; q++) { ` `        ``for` `(``int` `i = 0; i < A.size(); i++) { ` `            ``for` `(``int` `j = 0; j < A.size(); j++) { ` `                ``int` `maxi = ans[i][j]; ` `                ``if` `(i > 0) ` `                    ``maxi = max( ` `                        ``maxi, ans[i - 1][j]); ` `                ``if` `(j > 0) ` `                    ``maxi = max( ` `                        ``maxi, ans[i][j - 1]); ` `                ``if` `(i < A.size() - 1) ` `                    ``maxi = max( ` `                        ``maxi, ans[i + 1][j]); ` `                ``if` `(j < A.size() - 1) ` `                    ``maxi = max( ` `                        ``maxi, ans[i][j + 1]); ` `                ``A[i][j] = maxi; ` `            ``} ` `        ``} ` `        ``ans = A; ` `    ``} ` `    ``return` `ans; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``vector > B = { { 1, 2, 3 },  ` `                  ``{ 4, 5, 6 }, { 7, 8, 9 } }; ` `    ``printMatrix(getMaxNeighbour(B, 2)); ` `    ``return` `0; ` `} `

## Java

 `// Java implementation to find the  ` `// maximum neighbor element within ` `// the distance of less than K ` `import` `java.util.*; ` ` `  `class` `GFG{ ` ` `  `// Function to print the matrix ` `static` `void` `printMatrix(``int` `[][] A) ` `{ ` `     `  `    ``// Loop to iterate over the matrix ` `    ``for``(``int` `i = ``0``; i < A.length; i++) ` `    ``{ ` `       ``for``(``int` `j = ``0``; j < A[``0``].length; j++) ` `          ``System.out.print(A[i][j] + ``" "``); ` `       ``System.out.print(``'\n'``); ` `    ``} ` `} ` ` `  `// Function to find the maximum ` `// neighbor within the distance ` `// of less than equal to K ` `static` `int` `[][] getMaxNeighbour(``int` `[][] A, ` `                                ``int` `K) ` `{ ` `    ``int` `[][] ans = A; ` `     `  `    ``// Loop to find the maximum ` `    ``// element within the distance ` `    ``// of less than K ` `    ``for``(``int` `q = ``1``; q <= K; q++)  ` `    ``{ ` `       ``for``(``int` `i = ``0``; i < A.length; i++) ` `       ``{ ` `          ``for``(``int` `j = ``0``; j < A[``0``].length; j++)  ` `          ``{ ` `             ``int` `maxi = ans[i][j]; ` `             ``if` `(i > ``0``) ` `                 ``maxi = Math.max(maxi,  ` `                                 ``ans[i - ``1``][j]); ` `             ``if` `(j > ``0``) ` `                 ``maxi = Math.max(maxi,  ` `                                 ``ans[i][j - ``1``]); ` `             ``if` `(i < A.length - ``1``) ` `                 ``maxi = Math.max(maxi,  ` `                                 ``ans[i + ``1``][j]); ` `             ``if` `(j < A[``0``].length - ``1``) ` `                 ``maxi = Math.max(maxi, ` `                                 ``ans[i][j + ``1``]); ` `             ``A[i][j] = maxi; ` `          ``} ` `       ``} ` `       ``ans = A; ` `    ``} ` `    ``return` `ans; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``int` `[][] B = { { ``1``, ``2``, ``3` `},  ` `                   ``{ ``4``, ``5``, ``6` `}, ` `                   ``{ ``7``, ``8``, ``9` `} }; ` `                    `  `    ``printMatrix(getMaxNeighbour(B, ``2``)); ` `} ` `} ` ` `  `// This code is contributed by Princi Singh `

## C#

 `// C# implementation to find the  ` `// maximum neighbor element within ` `// the distance of less than K ` `using` `System; ` ` `  `class` `GFG{ ` ` `  `// Function to print the matrix ` `static` `void` `printMatrix(``int` `[,] A) ` `{ ` `     `  `    ``// Loop to iterate over the matrix ` `    ``for``(``int` `i = 0; i < A.GetLength(0); i++) ` `    ``{ ` `       ``for``(``int` `j = 0; j < A.GetLength(1); j++) ` `          ``Console.Write(A[i, j] + ``" "``); ` `       ``Console.Write(``'\n'``); ` `    ``} ` `} ` ` `  `// Function to find the maximum ` `// neighbor within the distance ` `// of less than equal to K ` `static` `int` `[,] getMaxNeighbour(``int` `[,] A, ` `                               ``int` `K) ` `{ ` `    ``int` `[,] ans = A; ` `     `  `    ``// Loop to find the maximum ` `    ``// element within the distance ` `    ``// of less than K ` `    ``for``(``int` `q = 1; q <= K; q++)  ` `    ``{ ` `       ``for``(``int` `i = 0; i < A.GetLength(0); i++) ` `       ``{ ` `          ``for``(``int` `j = 0; j < A.GetLength(1); j++) ` `          ``{ ` `             ``int` `maxi = ans[i, j]; ` `             ``if` `(i > 0) ` `                 ``maxi = Math.Max(maxi,  ` `                                 ``ans[i - 1, j]); ` `             ``if` `(j > 0) ` `                 ``maxi = Math.Max(maxi,  ` `                                 ``ans[i, j - 1]); ` `             ``if` `(i < A.GetLength(0) - 1) ` `                 ``maxi = Math.Max(maxi,  ` `                                 ``ans[i + 1, j]); ` `             ``if` `(j < A.GetLength(0) - 1) ` `                 ``maxi = Math.Max(maxi, ` `                                 ``ans[i, j + 1]); ` `             ``A[i, j] = maxi; ` `          ``} ` `       ``} ` `       ``ans = A; ` `    ``} ` `    ``return` `ans; ` `} ` ` `  `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``int` `[,] B = { { 1, 2, 3 },  ` `                  ``{ 4, 5, 6 }, ` `                  ``{ 7, 8, 9 } }; ` `                     `  `    ``printMatrix(getMaxNeighbour(B, 2)); ` `} ` `} ` ` `  `// This code is contributed by Princi Singh `

Output:

```7 8 9
8 9 9
9 9 9
```

Time Complexity: O(M*N*K)
Space Complexity: O(M*N)

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.

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.

Improved By : princi singh, nidhi_biet