Problem
You are given N points on a plane. Coordinates of each of them are integers from 0 to 10^5 inclusive. Each point is chosen randomly, equally likely among all possible points and independently of the others; in particular, points may coincide.
Choose four points at the vertices of a rectangle with horizontal and vertical sides and measure its area: what is the biggest area you can get?
Input format
N
x_1 y_1
x_2 y_2
...
x_N y_N
``` where `1 <= N <= 300'000` and `0 <= x_i, y_i <= 10^5`.
I am also given 2 examples: 6 points `(1, 1), (1, 3), (3, 1), (3, 3), (4, 1), (4, 3)` for which the answer is 6 and a file with 140k+ points with a big answer.
So first obviously I thought of doing what I'm told - choosing all possible four points, which would be O(N^4) and is way too slow. Then thought of choosing two points - the ones on the rectangle's diagonal and then checking if 2 other points exist, which is O(N^2 * log(N)) (log for checking if there's a point in a set) and is still too slow. The best I've got is sorting by the area of a rectangle formed by the point and origin (0, 0) (so sort by `x*y`) and then iterating `i = 0 .. N - 1` and `j = N - 1 .. i` and if the "theoretical maximum area" for either `i` or `j` is less than the already found maximum area, I break. This still runs way too slow for the 140k lines example. My code: https://paste.pythondiscord.com/SCBA.
Also I'm curious why would this be pointed out "Each point is chosen randomly, equally likely among all possible points and independently of the others". Maybe it's just to throw me off.