I have an array of random length full of random integers. For the sake of simplicity, they are ordered from the highest to the lowest.
I also have a target random integer.
I need to get all the combinations which sum is greater or equal to the target without using unnecesary numbers. Given this example:
nums = [10, 6, 5, 3, 2, 1, 1]
target = 8
I want this output:
10 (10 is higher or equal to 8, so there's no need to sum it)
6+5
6+3
6+2
6+1+1
5+3
5+2+1
5+2+1 (Note that this result is different from the previous since there are 2 1s)
I've tried a lot of recursive strategies but I can't find a correct answer. No specific language is required, pseudocode is welcome.
EDIT: Posting code
private static boolean filtrar(final CopyOnWriteArrayList<Padre> padres) {
if (sum(padres) < TARGET) {
return false;
}
int i;
for (i = padres.size(); i >= 0; i--) {
if (!filtrar(copiaPadresSinElemento(padres, i-1))) {
break;
}
}
// Solución óptima, no se puede quitar nada.
if (i == padres.size()) {
print(padres);
}
return true;
}
private static int sum(final CopyOnWriteArrayList<Padre> padres) {
int i = 0;
for (final Padre padre : padres) {
i += padre.plazas;
}
return i;
}
private static void print(final CopyOnWriteArrayList<Padre> padres) {
final StringBuilder sb = new StringBuilder();
for (final Padre padre : padres) {
sb.append(padre);
sb.append(", ");
}
final String str = sb.toString();
System.out.println(str);
}
private static CopyOnWriteArrayList<Padre> copiaPadresSinElemento(
final CopyOnWriteArrayList<Padre> padres, final int i) {
final CopyOnWriteArrayList<Padre> copia = new CopyOnWriteArrayList<Padre>();
for (int e = 0; e < padres.size(); e++) {
if (e != i) {
copia.add(padres.get(e));
}
}
return copia;
}
Padre is a simple object which contains a name for each number. Padre.plazas is the number.
The array right now is [6,4,4,4,3,3,3], and the target is 8.