combination code output needs explanation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • adri00713
    New Member
    • Apr 2013
    • 5

    combination code output needs explanation

    Code:
    import java.util.ArrayList;
        import java.util.List;
    public class CombinationApp {
    
            private static int[] numbers= {1,2,3,4,5,6,7,8,9,10};
            private static int[] sumsum= new int[numbers.length];
         
            private static int sum= 10;
         
            private static void solution(List<Integer> solution) {
         
                System.out.println(solution);
            }
         
            private static void solve(int[] numbers, int i, int sum, List<Integer> solution) {
         
                if (sum == 0) 
                    solution(solution);
                else
                    for (; i < numbers.length && sumsum[i] >= sum; i++) {
                        if (numbers[i] <= sum) {
                            solution.add(0, numbers[i]);
                            solve(numbers, i+1, sum-numbers[i], solution);
                            solution.remove(0);
                        }
                    }
            }
         
            public static void main(String[] args) {
         
                for (int s= 0, i= numbers.length; --i >= 0; ) {
                    sumsum[i]= numbers[i]+s;
                    s+= numbers[i];
                }
                solve(numbers, 0, sum, new ArrayList<Integer>());
            }
        }
    why is it that this is the output ?
    [4, 3, 2, 1]
    [7, 2, 1]
    [6, 3, 1]
    [5, 4, 1]
    [9, 1]
    [5, 3, 2]
    [8, 2]
    [7, 3]
    [6, 4]
    [10]
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Because that combination of values adds up to the target number, in this case 10. Since it's a recursive function, it repeatedly calls itself to add every combination of numbers in the array until it hits the target value or runs out of numbers to add.

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      It's a normal depth first algorithm so maybe revise that algorithm and have a look at the code again.
      Here is a variation without the main method setup
      Code:
          public static void main(String[] args) {
              int[] numberlist = {1,2,3,4,5,6,7,8,9,10};
              Set<String> answersList = new HashSet<String>();
              findSums(numberlist, 0, 0, 10, "", answersList);
              System.out.println(answersList);
              
          }
          
          public static void findSums(int[] list, int index, int current, int K, String previousList, Set<String> answers) {
              if (list.length < index || current > K)
                  return;
              for (int i = index; i < list.length; i++) {
                  if (current + list[i] == K) {
                      answers.add(previousList + " " + list[i]);
                  } else if (current + list[i] < K) {
                      findSums(list, i + 1, current + list[i], K, previousList + " " + list[i], answers);
                  }
              }
          }

      Comment

      Working...