title: Algorithm - Find maximum sum of two numbers in unsorted array that have a minimum distance
body: <p>I'm trying to find an algorithm that finds the maximum sum of two numbers that have a minimum distance <strong>D</strong> between them.</p>
  
  <p>For example, lets say we have this array of 8 numbers and the minimum distance for a sum is <strong>2</strong>:</p>
  
  <pre><code>9 4 6 2 8 7 5 6
  
  9 can be paired with  2, 8, 7, 5, 6
  4 can be paired with  8, 7, 5, 6
  6 can be paired with  7, 5, 6
  2 can be paired with  9 (from the left side), 5, 6
  8 can be paired with  9, 4 and 6
  etc..
  </code></pre>
  
  <p>From this array it is obvious that the maximum possible sum is 9+8 = 17</p>
  
  <p>Does anyone know of an efficient algorithm to do this, or has any idea? </p>
  
  <p>I have tried an algorithm which finds the max number, compares it with every possible else, and then checks every value that does not have a minimum distance from it with every possible else...but this is very slow when the array consists of many numbers and the especially when the minimum distance is large.</p>
  
  <p>Note: All numbers are <strong>positive</strong></p>
  
  <p>Thanks.</p>