Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding point, which distances sum to all other points on the line is the lowest

I've been wondering if it's possible to solve this problem in recursive or "divide and conquer" way. Here is visualisation of my problem:

enter image description here

Input:
22 // point no 1
35 // point no 2
5  // ...
44
45
20
46

Output: 2 // point with number 2 has got the lowest sum (87)

I know how to do this in an iterative way, but I'm thinking about something more optimal.

like image 590
Paulina Avatar asked Oct 23 '12 08:10

Paulina


People also ask

How do you find the least distance between two points?

The shortest distance between two points is a straight line. This distance can be calculated by using the distance formula. The distance between two points ( x 1 , y 1 ) and ( x 2 , y 2 ) can be defined as d = ( x 2 − x 1 ) 2 + ( y 2 − y 1 ) 2 .

How do you find the distance between a point and a line?

To calculate the distance between a point and a straight line we could go step by step (calculate the segment perpendicular to the line from the line to the point and the compute its length) or we could simply use this 'handy-dandy' equation: d = |Ax1 + By1 + C | / √(A2 + B2) where the line is given by Ax+By+C = 0 and ...


1 Answers

This is called the median value. Just sort the values and select the center. If n is even, any of the two center values will do.

There are also O(n) algorithms for calculating the median.

like image 169
Henrik Avatar answered Oct 26 '22 05:10

Henrik