Best meeting point in 2D binary array | Java program to find best meeting point in 2D array ; Find all members home 's position ; Sort positions so we can find most beneficial point ; middle position will always beneficial for all group members but it will be sorted which we have already done ; Now find total distance from best meeting point ( x , y ) using Manhattan Distance formula ; Driver code
import java . util . * ; class GFG { static int ROW = 3 ; static int COL = 5 ; static int minTotalDistance ( int grid [ ] [ ] ) { if ( ROW == 0 COL == 0 ) return 0 ; Vector < Integer > vertical = new Vector < Integer > ( ) ; Vector < Integer > horizontal = new Vector < Integer > ( ) ; for ( int i = 0 ; i < ROW ; i ++ ) { for ( int j = 0 ; j < COL ; j ++ ) { if ( grid [ i ] [ j ] == 1 ) { vertical . add ( i ) ; horizontal . add ( j ) ; } } } Collections . sort ( vertical ) ; Collections . sort ( horizontal ) ; int size = vertical . size ( ) / 2 ; int x = vertical . get ( size ) ; int y = horizontal . get ( size ) ; int distance = 0 ; for ( int i = 0 ; i < ROW ; i ++ ) for ( int j = 0 ; j < COL ; j ++ ) if ( grid [ i ] [ j ] == 1 ) distance += Math . abs ( x - i ) + Math . abs ( y - j ) ; return distance ; } public static void main ( String [ ] args ) { int grid [ ] [ ] = { { 1 , 0 , 1 , 0 , 1 } , { 0 , 1 , 0 , 0 , 0 } , { 0 , 1 , 1 , 0 , 0 } } ; System . out . println ( minTotalDistance ( grid ) ) ; } }