455,800 Members | 1,404 Online
Need help? Post your question and get tips & solutions from a community of 455,800 IT Pros & Developers. It's quick & easy.

# combination code output needs explanation

 P: 5 Expand|Select|Wrap|Line Numbers 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 solution) {               System.out.println(solution);         }           private static void solve(int[] numbers, int i, int sum, List 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());         }     }     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] Apr 3 '13 #1