Finding nearest element in a set of integers
<p>I have following problem to solve: </p>

<p>Given N integers forming set S and another integer A (not neccesarily same as any of the N integers given), find integer from set S that is nearest to integer A.</p>

<p>I first thought this is a NNS (nearest neighbor Search) problem, but in NNS, integer A must be from set S as well, which in my case it doesn't have to be. </p>

<p>Then I thought of putting each integer from S into a binary search tree and searching for first occurence where one of children is smaller than query and parent is bigger than query, but I don't know if this will work.</p>

<p>Which data structure should I use? Thank you.</p>

<p>EDIT: forgot to mention I need this to be better than O(n), O(logn) is good enough. Thus I can't use linear search.</p>