title: given an input array and sum, return the minimum elements need to get sum
body: <p>The problem is a variation of <code>selection algorithm</code> in selecting the fewest number of items whose sum is at least some value, N. For example you might want to have a list of items whose sum is 5,000 or more, but you don’t want more items than you have to take from an input list. So if one number is 5,001, then your output list will contain a single number.</p>
  
  <p>another example: <code>Input list {3,4,5,1,2} target = {8}.</code>
  <code>output list will be : {5,3}</code>.</p>
  
  <p>This problem has been shown to solve using <code>binaryHeap</code> in this <a href="http://blog.mischel.com/2013/10/03/uses-for-heaps/" rel="nofollow">excellent series of posts</a>;
  but when I implemented in <code>Java</code>, I do not get the desired the output. I get the entire <code>input list</code>.
  I am not able to find out the exact cause of the problem. Here is my code;</p>
  
  <pre><code>public class SelectTopSum {
      public static void main(String[] args){
          int[] arr = {5,2,10,1,33};
          int target=33;
  
          System.out.println(Arrays.toString(selectTop(arr, target)));
      }
  
      private static int[] selectTop(int[] arr, int sum) {
          //get max heap
          PriorityQueue&lt;Integer&gt; pq = new PriorityQueue&lt;&gt;(10, new Comparator&lt;Integer&gt;() {
              @Override
              public int compare(Integer o1, Integer o2) {
                  return o2.compareTo(o1);
              }
          });
          int runningTot=0;
  
          for (int i : arr) {
              if (runningTot&lt;sum) {
                  runningTot+=i;
                  pq.add(i);
              }
              else if (i &gt; pq.peek()) {
                  runningTot-=pq.remove();
                  runningTot+=i;
                  pq.add(i);
              }
              while (runningTot-pq.peek()&gt;=sum) {
                  runningTot-=pq.remove();
              }
          }
  
          //System.out.println("priority queue is "+ pq);
  
          int[] result=new int[pq.size()];
          int i=0;
          while (!pq.isEmpty()) {
              result[i]=pq.remove();
              i++;
          }
          return result;
      }
  }
  </code></pre>