title: How to balance between two arrays such as the difference is minimized?
body: <p>I have an array <code>A[]={3,2,5,11,17}</code> and <code>B[]={2,3,6}</code>, size of B is always less than A. Now I have to map from every element B to <em>distinct</em> elements of A such that the total difference <code>sum( abs(Bi-Aj) )</code> becomes minimum (Where Bi has been mapped to Aj). What is the type of algorithm?</p>
  
  <p>For the example input, I could select, <code>2-&gt;2=0</code> , <code>3-&gt;3=0</code> and then <code>6-&gt;5=1</code>. So the total cost is 0+0+1 = 1. I have been thinking sorting both the arrays and then take the first <code>sizeof B</code> elements from the A. Will this work?</p>