Maximum Manhattan distance between a distinct pair from N coordinates | Java program for the above approach ; Pair class ; 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
import java . io . * ; import java . util . * ; class GFG { public static class Pair { int x ; int y ; Pair ( int x , int y ) { this . x = x ; this . y = y ; } } static void MaxDist ( ArrayList < Pair > A , int N ) { int maximum = Integer . MIN_VALUE ; for ( int i = 0 ; i < N ; i ++ ) { int sum = 0 ; for ( int j = i + 1 ; j < N ; j ++ ) { sum = Math . abs ( A . get ( i ) . x - A . get ( j ) . x ) + Math . abs ( A . get ( i ) . y - A . get ( j ) . y ) ; maximum = Math . max ( maximum , sum ) ; } } System . out . println ( maximum ) ; } public static void main ( String [ ] args ) { int n = 3 ; ArrayList < Pair > al = new ArrayList < > ( ) ; Pair p1 = new Pair ( 1 , 2 ) ; al . add ( p1 ) ; Pair p2 = new Pair ( 2 , 3 ) ; al . add ( p2 ) ; Pair p3 = new Pair ( 3 , 4 ) ; al . add ( p3 ) ; MaxDist ( al , n ) ; } }