import java.util.ArrayList;
import java.util.Arrays;
import java.util.Stack;
public class subArrayOfgivenValue {
public static void main(String[] args) {
int[] candidates = { 5, 5, 10, 2, 3 };
int target = 15;
ArrayList<ArrayList<Integer>> res = combinationSum(candidates, target);
for (ArrayList<Integer> list : res) {
System.out.println(list);
}
}
public static ArrayList<ArrayList<Integer>> combinationSum(int[] num, int target) {
ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
// Arrays.sort(num);
Stack<Integer> stk = new Stack<Integer>();
findCombination(result, 0, target, stk, num);
return result;
}
private static void findCombination(ArrayList<ArrayList<Integer>> result, int
index,int target, Stack<Integer> stk, int[] num) {
if (target == 0) {
result.add(new ArrayList<Integer>(stk));
return;
} else {
for (int i = index; i < num.length; i++) {
if (num[i] > target)
continue;
stk.push(num[i]);
findCombination(result, i + 1, target - num[i], stk, num);
stk.pop();
}
}
}
}