I have a list of Integer with increasing numbers (in order, no duplicates) like

3, 5, 11, 16, 25, 33, 40

I want to know how many pairs I could create with the following condition:

* the difference between the two numbers can not be lower then 7
* the difference between the two numbers can not be higher then 17
* I can use every number only once

With above example I could create the following pairs (ignoring the last condition)  

(3, 11)  

(3, 16)  

(5, 16)  

(11, 25)  

(16, 25)  

(16, 33)  

(25, 33)  

(25, 40)  

(33, 40)

But as I can use every number only once, I could get for example  

(3, 16)  

(25, 33)  

**or**  

(3, 11)  

(5, 16)  

(33, 40)

What I want, would be the second example, because it's the most possible count of pairs I could get.

What I have so far:

```
int CountPairs( std::vector< int > listInt )
{
   std::vector< std::tuple< int, int > > listPairs;

   for( std::vector<int>::const_iterator iter = listInt.begin();
      iter != listInt.end();
      ++iter )
   {
      int val = *iter;
      std::vector<int>::const_iterator compareIter = iter;
      compareIter++;

      for( ; compareIter != listInt.end(); ++compareIter )
      {
         int compareVal = *compareIter;

         int result = compareVal - val;

         if( result >= 7 && result <= 17 )
         {
            listPairs.push_back( make_tuple( val, compareVal ) );
         }

         if( result > 17 )
            break;
      }
   }

   if( listPairs.size() <= 1 )
   {
      return listPairs.size();
   }

   //...
}

```

After this I have at least a list of all possible combinations, but I am not sure how to proceed or if there is a better way.

Does anybody have a solution?