Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Let's say the arrays are <code>A = {A[1] ... A[n]}</code>, and <code>B = {B[1] ... B[n]}</code>, and the pairwise sum array is <code>C = {A[i] + B[j], where 1 &lt;= i &lt;= n, 1 &lt;= j &lt;= n}</code> which has <code>n^2</code> elements and we need to find its median.</p> <p>Median of <code>C</code> must be an element of the array <code>D = {A[1] + B[n], A[2] + B[n - 1], ... A[n] + B[1]}</code>: if you fix <code>A[i]</code>, and consider all the sums <code>A[i] + B[j]</code>, you would see that the <em>only</em> <code>A[i] + B[j = n + 1 - i]</code> (which is one of <code>D</code>) <em>could be</em> the median. That is, it may not be the median, but if it is not, then all other <code>A[i] + B[j]</code> are also not median. </p> <p>This can be proved by considering all <code>B[j]</code> and count the <em>number of values</em> that are <strong>lower</strong> and <em>number of values</em> that are <strong>greater</strong> than <code>A[i] + B[j]</code> (we can do this quite accurately because the two arrays are sorted -- the calculation is a bit messy thought). You'd see that for <code>A[i] + B[n + 1 - j]</code> these two counts are most "balanced".</p> <p>The problem then reduces to finding median of <code>D</code>, which has only <code>n</code> elements. An algorithm such as <a href="http://en.wikipedia.org/wiki/Selection_algorithm" rel="nofollow">Hoare's</a> will work.</p> <p><strong>UPDATE</strong>: this answer is wrong. The real conclusion here is that the <strong>median</strong> is one of <code>D</code>'s element, but then <em><code>D</code>'s median</em> is the not the same as <code>C</code>'s median.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload