A simple way to solve this problem in O (n) is to use inverse in a circle .
General scheme
Select any point O (x, y) from the set to the origin and select a circle of radius r with the point you select in the center. The calculations will be simplified if all points are transferred to the sum (-x, -y), so that point O becomes the beginning.
Once you have selected the source and the circle, perform the inverse operation relative to the selected circle to all other points in the set. In the coordinates, we compare the point P (x, y) with the new point P 'with the coordinates (x',y') = (x,y)*r^2/(x^2+y^2) . The origin of O (0,0) goes to an infinite point.
With the inversion transformation, the task is to find a line with the required properties (it passes through two of the converted points, because the line always passes through an infinitely remote point, in addition, the line should divide the remaining converted points into two equal sets).
Note that the inversion maps a set of 3 collinear and 4 concentric points to a set of 3 collinear and 4 concentric points, so there are no 3 collinear points and not 4 concentric points in the converted set.
To construct a circle, we then perform a reverse inversion (which coincides with the original inversion, since the inversion in the circle is an involution, a self-inverse transformation). This draws the line back to the circle passing through the first selected point and two other points from the original set. Half of the remaining points are inside the circle, half outside.
Corresponding task with lines
Now we have led the problem to the following: taking into account 2n + 2 points on the plane, find a line passing through 2 such points that of the remaining 2n points, n are on one side of the line and n on the other.
We can answer this question O (n) times as follows. First, we need one point on the boundary of the convex hull of the set (we do not need the entire convex hull). We can find such a point P by finding point (s) in a set with a minimum value of x coordinates, which can be done O (n) times using the Quickselect algorithm.
Now we know that all our points are in the half-space to the right of P. The idea of ββthe next step is to find the point Q with the median angle of all points relative to the horizontal line h through P. Again, this can be done in O (n) times using the Quickselect algorithm. There cannot be two points with the same angle, because there cannot be three points that are collinear.
The line PQ passes through two points and divides the remaining points R into two equal parts, depending on whether the angle RPh is greater than QPh or less than QPh (negative angles below h).
One small problem arises: if the line passes through the origin, then the inversion transfers it to another line, and not to the circle. However, this situation cannot arise due to the fact that it would mean that we had 3 collinear points in the initial set, which would not be guaranteed as a result of the statement of the problem.