title: Finding N closest numbers
body: <p>Have a  2-dimensional array, like -</p>
  
  <pre>
  a[0] = [ 0 , 4 , 9 ]
  a[1] = [ 2 , 6 , 11 ]
  a[2] = [ 3 , 8 , 13 ]
  a[3] = [ 7 , 12 ]</pre>
  
  <p>Need to select one element from each of the sub-array in a way that the resultant set of numbers are closest, that is the difference between the highest number and lowest number in the set is minimum.</p>
  
  <p>The answer to the above will be = <code>[ 9 , 6 , 8 , 7 ]</code>.</p>
  
  <p>Have made an algorithm, but don't feel its a good one.
  What would be a efficient algorithm to do this in terms of time and space complexity?</p>
  
  <p><b>EDIT - My Algorithm </b>(in python)-
  <pre>
  INPUT - Dictionary : table{}
  OUTPUT - Dictionary : low_table{}
  #
  N = len(table)
  for word_key in table:
      for init in table[word_key]:
          temp_table = copy.copy(table)
          del temp_table[word_key]
          per_init = copy.copy(init)
          low_table[init]=[]
          for ite in range(N-1):
              min_val = 9999
              for i in temp_table:
                  for nums in temp_table[i]:
                      if min_val > abs(init-nums):
                          min_val = abs(init-nums)
                          del_num = i
                          next_num = nums
              low_table[per_init].append(next_num)
              init = (init+next_num)/2
              del temp_table[del_num]
  lowest_val = 99
  lowest_set = []
  for x in low_table:
      low_table[x].append(x)
      low_table[x].sort()
      mini = low_table[x][-1]-low_table[x][0]
      if mini &lt; lowest_val:
          lowest_val = mini
          lowest_set = low_table[x]
  print lowest_set
  </pre></p>