# Finding the point with minimum x-ordinate, lying between two y-ordinates

Given a set of points $$P=p_1,p_2,..p_n$$ in $$R^2$$ in where $$p_i=(x_i,y_i)$$,finding the point with smallest x-ordinate having y-ordinates between $$y_1$$ and $$y_2$$, where $$y_1$$ and $$y_2$$ are given as inputs. I can compare the point with other points which gives me an $$O(n)$$ time algorithm ? Can this be improved any further ?

• In a simple list ordered from left to right, I can find the leftmost in $O(1)$: specify what is represented how. Apr 23, 2020 at 5:04
• The answer depends on how the input (the list of points) is represented, and can't be answered without that information. If you'd like your question to be answered, please edit it to provide the requested information. Also, when you say $O(n)$, $O(n)$ what? $O(n)$ time? $O(n)$ comparisons?
– D.W.
Apr 24, 2020 at 18:18
• Updated the question. Apr 25, 2020 at 12:47

Not unless you assume something more. Without further assumptions, if you don't check all $$n$$ values, the minimum can be some of the unchecked points.

If you know there is just one minimum in (sorted) range, a variant of binary search (the minimum is the point where $$x_{k - 1} > x_k < x_{k + 1}$$ or one of the endpoints; if $$x_k > x_{k + 1}$$ the minimum is to the right, and so on) gets it in time $$O(\log n)$$.

If the points are provided in an arbitrary order (not sorted or anything), then no, you cannot do better than $$O(n)$$ time. You must examine every point. This is easily proven using an adversary argument: if there is any point your algorithm doesn't inspect, then that point could have had a valid $$y$$-value and a smaller $$x$$-value than whatever your algorithm outputs, meaning that any such algorithm will have inputs on which it outputs the wrong answer.

• What if we presort the points in order of the x-ordinates in ascending order ? Apr 26, 2020 at 21:21
• @ArkapravaPaul, I can only answer the question that was asked, and that is not part of the problem specification in the answer. I believe you should be able to figure out the answer to that variant as well on your own: try adapting the ideas in my answer. What's the fastest algorithm you can find? Can you come up with an adversary argument?
– D.W.
Apr 27, 2020 at 4:17

A Computational Geometry answer would include a description of a data structure and analysed algorithms for preprocessing/set-up and queries.
There is Edward McCreight's Priority Search Tree, use a min heap on $$x$$ with $$y$$ medians for telling left from right. (The first point you encounter between $$y_1$$ and $$y_2$$ is the one with the lowest $$x$$ in range.)
Not storing the additional median in each node, but using the point's coordinate works almost as well for well behaved sets.

• Been there, done that more than one third of a century ago. Apr 28, 2020 at 20:57