The problem I am facing is as follows : Given two arrays $A$ and $B,$ I would like to find a threshold $t,$ satisfying: the number of elements of $A$ that are less than $t$ equals the number of elements of $B$ that are greater than $t.$ I am pretty sure that the solution of my problem is the median of some array formed by $A$ and $B,$ but I don't see exactly how.
To generalize, we are looking for a threshold minimizing the difference between the number of $A$'s lesser elements and the number of $B$'s greater elements.
Thanks in advance for your replies.
Hehe. I'm adding a lot of answers, but I think each answer here has value, and represents a different approach. Here's an approach that uses sort of a cumulative distribution function for $A$, and a "de-cumulative" distribution function for $B$. The idea is to find the smallest point in either array, the biggest point in either array, construct an equal-sized grid of values for numbers in the arrays $A$ and $B$, do a difference, then find the min of the absolute value of the difference. Here's the Python code:
This code only uses implied loops, such as to find the
a_cdfandb_ddfvariables. Now the downer to this approach is that you don't necessarily know if your grid is fine enough. Right now, I just have it running on integers. You might find that if you use a fine enough grid, you can find solutions more readily than not. You can easily change the grid coarseness by changing thenp.arange(min_ab, max_ab + 1, 1)call: make the last argument smaller.