Minimum sum of all absolute differences of same column elements in adjacent rows in a given Matrix
Last Updated :
17 Feb, 2022
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++
#include <bits/stdc++.h>
using namespace std;
int calcDist( int N, int M, vector<vector< int > > mat)
{
int ans = INT_MAX;
for ( int i = 0; i < N - 1; i++) {
int dist = 0;
for ( int j = 0; j < M; j++) {
dist += abs (mat[i][j] - mat[i + 1][j]);
}
ans = min(ans, dist);
}
return ans;
}
int main()
{
vector<vector< int > > mat = { { 1, 4, 7, 10 },
{ 2, 5, 8, 11 },
{ 6, 9, 3, 2 } };
cout << calcDist(mat.size(), mat[0].size(), mat);
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static int
calcDist( int N, int M,
ArrayList<ArrayList<Integer> > mat)
{
int ans = Integer.MAX_VALUE;
for ( int i = 0 ; i < N - 1 ; i++)
{
int dist = 0 ;
for ( int j = 0 ; j < M; j++) {
dist += Math.abs(mat.get(i).get(j)
- mat.get(i + 1 ).get(j));
}
ans = Math.min(ans, dist);
}
return ans;
}
public static void main(String[] args)
{
ArrayList<ArrayList<Integer> > mat
= new ArrayList<ArrayList<Integer> >();
ArrayList<Integer> temp1 = new ArrayList<Integer>(
Arrays.asList( 1 , 4 , 7 , 10 ));
ArrayList<Integer> temp2 = new ArrayList<Integer>(
Arrays.asList( 2 , 5 , 8 , 11 ));
ArrayList<Integer> temp3 = new ArrayList<Integer>(
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));
}
}
|
Python3
INT_MAX = 2147483647
def calcDist(N, M, mat):
ans = INT_MAX
for i in range ( 0 , N - 1 ):
dist = 0
for j in range ( 0 , M):
dist + = abs (mat[i][j] - mat[i + 1 ][j])
ans = min (ans, dist)
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))
|
C#
using System;
class GFG
{
static int calcDist( int N, int M, int [, ] mat)
{
int ans = Int32.MaxValue;
for ( int i = 0; i < N - 1; i++) {
int dist = 0;
for ( int j = 0; j < M; j++) {
dist += Math.Abs(mat[i, j] - mat[i + 1, j]);
}
ans = Math.Min(ans, dist);
}
return ans;
}
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));
}
}
|
Javascript
<script>
function calcDist(N, M, mat)
{
let ans = Number.MAX_VALUE;
for (let i = 0; i < N - 1; i++) {
let dist = 0;
for (let j = 0; j < M; j++) {
dist += Math.abs(mat[i][j] - mat[i + 1][j]);
}
ans = Math.min(ans, dist);
}
return ans;
}
let mat = [[1, 4, 7, 10],
[2, 5, 8, 11],
[6, 9, 3, 2]];
document.write(calcDist(mat.length, mat[0].length, mat));
</script>
|
Time Complexity: O(N*M)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...