Maximum Manhattan distance between a distinct pair from N coordinates | C ++ program for the above approach ; Function to calculate the maximum Manhattan distance ; Stores the maximum distance ; Find Manhattan distance using the formula | x1 - x2 | + | y1 - y2 | ; Updating the maximum ; Driver Code ; Given Co - ordinates ; Function Call
#include <bits/stdc++.h> NEW_LINE using namespace std ; void MaxDist ( vector < pair < int , int > > & A , int N ) { int maximum = INT_MIN ; for ( int i = 0 ; i < N ; i ++ ) { int sum = 0 ; for ( int j = i + 1 ; j < N ; j ++ ) { sum = abs ( A [ i ] . first - A [ j ] . first ) + abs ( A [ i ] . second - A [ j ] . second ) ; maximum = max ( maximum , sum ) ; } } cout << maximum ; } int main ( ) { int N = 3 ; vector < pair < int , int > > A = { { 1 , 2 } , { 2 , 3 } , { 3 , 4 } } ; MaxDist ( A , N ) ; return 0 ; }