Maximum Manhattan distance between a distinct pair from N coordinates | C ++ program for the above approach ; Function to calculate the maximum Manhattan distance ; Vectors to store maximum and minimum of all the four forms ; Sorting both the vectors ; 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 ) { vector < int > V ( N ) , V1 ( N ) ; for ( int i = 0 ; i < N ; i ++ ) { V [ i ] = A [ i ] . first + A [ i ] . second ; V1 [ i ] = A [ i ] . first - A [ i ] . second ; } sort ( V . begin ( ) , V . end ( ) ) ; sort ( V1 . begin ( ) , V1 . end ( ) ) ; int maximum = max ( V . back ( ) - V . front ( ) , V1 . back ( ) - V1 . front ( ) ) ; cout << maximum << endl ; } int main ( ) { int N = 3 ; vector < pair < int , int > > A = { { 1 , 2 } , { 2 , 3 } , { 3 , 4 } } ; MaxDist ( A , N ) ; return 0 ; }