 GFG App
Open App Browser
Continue

# Minimum sum of all absolute differences of same column elements in adjacent rows in a given Matrix

Given a matrix mat[][] having N rows and M columns, the task is to find the minimum distance between two adjacent rows where the distance between two rows is defined as the sum of all absolute differences between two elements present at the same column in the two rows.

Examples:

Input: mat[][] = {{1, 4, 7, 10}, {2, 5, 8, 11}, {6, 9, 3, 12}}
Output: 4
Explanation: The distance between the first two rows can be calculated as (2-1) + (5-4) + (8-7) + (11-10) = 4. Similarly, the distance between the 2nd and 3rd row can be calculated as (6-2) + (9-5) + (8-3) + (12-11) = 14. Hence, the minimum distance among all adjacent rows is 4.

Input: mat[][] = {{1, 25, 81}, {2, 36, 100}, {9, 49, 50}, {16, 64, 25}}
Output : 31

Approach:  The given problem is an implementation-based problem that can be solved by iterating through the matrix row-wise and calculating the distances over all pairs of adjacent rows. Maintain the minimum of all the calculated distances in a variable which is the required answer.

Below is the implementation of the above approach :

## C++

 `// C++ program of the above approach.` `#include ` `using` `namespace` `std;`   `// Function to find minimum distance` `// between two adjacent rows in mat` `int` `calcDist(``int` `N, ``int` `M, vector > mat)` `{` `    ``// Stores the required value` `    ``int` `ans = INT_MAX;`   `    ``// Loop to traverse all the` `    ``// pair of rows in mat[][]` `    ``for` `(``int` `i = 0; i < N - 1; i++) {` `        ``// Stores the distance` `        ``int` `dist = 0;`   `        ``// Loop to calculate` `        ``// the distance` `        ``for` `(``int` `j = 0; j < M; j++) {` `            ``dist += ``abs``(mat[i][j] - mat[i + 1][j]);` `        ``}`   `        ``// Update ans` `        ``ans = min(ans, dist);` `    ``}`   `    ``// Return Answer` `    ``return` `ans;` `}`   `// C++ program of the above approach` `int` `main()` `{` `    ``vector > mat = { { 1, 4, 7, 10 },` `                                 ``{ 2, 5, 8, 11 },` `                                 ``{ 6, 9, 3, 2 } };` `    ``cout << calcDist(mat.size(), mat.size(), mat);`   `    ``return` `0;` `}`

## Java

 `// JAVA program of the above approach.` `import` `java.util.*;` `class` `GFG` `{`   `  ``// Function to find minimum distance` `  ``// between two adjacent rows in mat` `  ``public` `static` `int` `    ``calcDist(``int` `N, ``int` `M,` `             ``ArrayList > mat)` `  ``{`   `    ``// Stores the required value` `    ``int` `ans = Integer.MAX_VALUE;`   `    ``// Loop to traverse all the` `    ``// pair of rows in mat[][]` `    ``for` `(``int` `i = ``0``; i < N - ``1``; i++) ` `    ``{`   `      ``// Stores the distance` `      ``int` `dist = ``0``;`   `      ``// Loop to calculate` `      ``// the distance` `      ``for` `(``int` `j = ``0``; j < M; j++) {` `        ``dist += Math.abs(mat.get(i).get(j)` `                         ``- mat.get(i + ``1``).get(j));` `      ``}`   `      ``// Update ans` `      ``ans = Math.min(ans, dist);` `    ``}`   `    ``// Return Answer` `    ``return` `ans;` `  ``}`   `  ``// JAVA program of the above approach` `  ``public` `static` `void` `main(String[] args)` `  ``{` `    ``ArrayList > mat` `      ``= ``new` `ArrayList >();` `    ``ArrayList temp1 = ``new` `ArrayList(` `      ``Arrays.asList(``1``, ``4``, ``7``, ``10``));` `    ``ArrayList temp2 = ``new` `ArrayList(` `      ``Arrays.asList(``2``, ``5``, ``8``, ``11``));` `    ``ArrayList temp3 = ``new` `ArrayList(` `      ``Arrays.asList(``6``, ``9``, ``3``, ``2``));` `    ``mat.add(temp1);` `    ``mat.add(temp2);` `    ``mat.add(temp3);`   `    ``System.out.print(` `      ``calcDist(mat.size(), mat.get(``0``).size(), mat));` `  ``}` `}`   `// This code is contributed by Taranpreet`

## Python3

 `# python3 program of the above approach.` `INT_MAX ``=` `2147483647`   `# Function to find minimum distance` `# between two adjacent rows in mat` `def` `calcDist(N, M, mat):`   `    ``# Stores the required value` `    ``ans ``=` `INT_MAX`   `    ``# Loop to traverse all the` `    ``# pair of rows in mat[][]` `    ``for` `i ``in` `range``(``0``, N ``-` `1``):` `      `  `        ``# Stores the distance` `        ``dist ``=` `0`   `        ``# Loop to calculate` `        ``# the distance` `        ``for` `j ``in` `range``(``0``, M):` `            ``dist ``+``=` `abs``(mat[i][j] ``-` `mat[i ``+` `1``][j])`   `        ``# Update ans` `        ``ans ``=` `min``(ans, dist)`   `    ``# Return Answer` `    ``return` `ans`   `if` `__name__ ``=``=` `"__main__"``:`   `    ``mat ``=` `[[``1``, ``4``, ``7``, ``10``],` `           ``[``2``, ``5``, ``8``, ``11``],` `           ``[``6``, ``9``, ``3``, ``2``]]`   `    ``print``(calcDist(``len``(mat), ``len``(mat[``0``]), mat))`   `    ``# This code is contributed by rakeshsahni`

## C#

 `// C# program of the above approach.` `using` `System;` `class` `GFG ` `{`   `  ``// Function to find minimum distance` `  ``// between two adjacent rows in mat` `  ``static` `int` `calcDist(``int` `N, ``int` `M, ``int``[, ] mat)` `  ``{`   `    ``// Stores the required value` `    ``int` `ans = Int32.MaxValue;`   `    ``// Loop to traverse all the` `    ``// pair of rows in mat[][]` `    ``for` `(``int` `i = 0; i < N - 1; i++) {` `      ``// Stores the distance` `      ``int` `dist = 0;`   `      ``// Loop to calculate` `      ``// the distance` `      ``for` `(``int` `j = 0; j < M; j++) {` `        ``dist += Math.Abs(mat[i, j] - mat[i + 1, j]);` `      ``}`   `      ``// Update ans` `      ``ans = Math.Min(ans, dist);` `    ``}`   `    ``// Return Answer` `    ``return` `ans;` `  ``}`   `  ``// Criver code` `  ``public` `static` `void` `Main()` `  ``{` `    ``int``[, ] mat = { { 1, 4, 7, 10 },` `                   ``{ 2, 5, 8, 11 },` `                   ``{ 6, 9, 3, 2 } };` `    ``Console.Write(calcDist(mat.GetLength(0),` `                           ``mat.GetLength(1), mat));` `  ``}` `}`   `// This code is contributed by Samim Hossain Mondal.`

## Javascript

 ``

Output

`4`

Time Complexity: O(N*M)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up