> 
> **Possible Duplicate:**  
> 
> [Finding all possible combinations of numbers to reach a given sum](https://stackoverflow.com/questions/4632322/finding-all-possible-combinations-of-numbers-to-reach-a-given-sum) 
> 
> 
> 

I have to create method which from array of numbers selects numbers, which sum will be exact as required one or if such doesn't exist select the minimal greater one.
What would be the algorithm of this function?

```
public int[] selectExactSum(int[] X, int SUM) {
}

```

example:
numbers are: {5, 2, 8, 4, 6} and required sum is 12.

The result would be: {2, 4, 6}

If required sum is 13, the result would be:{2, 8, 4} - so, the sum would be in this case 14 - the first minimal greater one.

If Required sum is 15, the possible results would be: {5, 2, 8} or {5, 4, 6}. In this case return the one of your choice - probably the first one you get.

What would be the algorithm for custom numbers and sum?

Thanks,
Simon