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 ; ArrayLists to store maximum and minimum of all the four forms ; Sorting both the ArrayLists ; 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 ) { ArrayList < Integer > V = new ArrayList < > ( ) ; ArrayList < Integer > V1 = new ArrayList < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { V . add ( A . get ( i ) . x + A . get ( i ) . y ) ; V1 . add ( A . get ( i ) . x - A . get ( i ) . y ) ; } Collections . sort ( V ) ; Collections . sort ( V1 ) ; int maximum = Math . max ( ( V . get ( V . size ( ) - 1 ) - V . get ( 0 ) ) , ( V1 . get ( V1 . size ( ) - 1 ) - V1 . get ( 0 ) ) ) ; 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 ) ; } }