Minimum Sum of Euclidean Distances to all given Points | Java program to implement the above approach ; Function to calculate Euclidean distance ; Function to calculate the minimum sum of the euclidean distances to all points ; Calculate the centroid ; Calculate distance of all points ; Driver Code ; Initializing the points
class GFG { static double find ( double x , double y , int [ ] [ ] p ) { double mind = 0 ; for ( int i = 0 ; i < p . length ; i ++ ) { double a = p [ i ] [ 0 ] , b = p [ i ] [ 1 ] ; mind += Math . sqrt ( ( x - a ) * ( x - a ) + ( y - b ) * ( y - b ) ) ; } return mind ; } static double getMinDistSum ( int [ ] [ ] p ) { double x = 0 , y = 0 ; for ( int i = 0 ; i < p . length ; i ++ ) { x += p [ i ] [ 0 ] ; y += p [ i ] [ 1 ] ; } x = x / p . length ; y = y / p . length ; double mind = find ( x , y , p ) ; return mind ; } public static void main ( String [ ] args ) { int [ ] [ ] vec = { { 0 , 1 } , { 1 , 0 } , { 1 , 2 } , { 2 , 1 } } ; double d = getMinDistSum ( vec ) ; System . out . print ( d + "NEW_LINE"); } }