Reputation: 11
I have a list of numbers: [10, 13, 15]
. I am trying to find the combinations of numbers in the list that add up to or are less than the target of 28.
Currently I have a recursive method:
public void combinations(ArrayList<Integer>data,int fromIndex, int endIndex)
{
int sum = 0;
int target = 28;
ArrayList<Integer>results = new ArrayList<Integer>();
if(fromIndex == endIndex)
{
return;
}
for(int currentIndex = fromIndex; currentIndex < endIndex; currentIndex++)
{
if(sum + data.get(currentIndex) <= target)
{
results.add(data.get(currentIndex));
sum +=data.get(currentIndex);
}
}
System.out.println(results);
combinations(data, fromIndex + 1, endIndex);
}
Currently this outputs:
[10, 13],[13, 15],[15]
which are correct and I understand why I am getting these solutions as my recursive method has a +1. However other solutions such as [10],[13],[10,10]
ect are not included and I was wondering how I would go about implementing this, would I need to change my increments in my recursive method?
Upvotes: 0
Views: 54
Reputation: 1705
public static void combinations(ArrayList<Integer> arr, ArrayList<ArrayList<Integer>> ans, int startIndex, int endIndex, int sum) {
if(startIndex > endIndex) {
for(ArrayList<Integer> x : ans) {
System.out.println(x);
}
return;
}
ArrayList<Integer> newTemp;
ArrayList<ArrayList<Integer>> newAns = new ArrayList<ArrayList<Integer>>();
for(ArrayList<Integer> x : ans) {
newAns.add(x);
}
for(ArrayList<Integer> x : ans) {
int s = 0;
newTemp = new ArrayList<Integer>();
for(Integer v : x) {
newTemp.add(v);
s+=v;
}
if(s + arr.get(startIndex) <= sum) {
newTemp.add(arr.get(startIndex));
newAns.add(newTemp);
}
}
if(arr.get(startIndex) <= sum ) {
newTemp = new ArrayList<Integer>();
newTemp.add(arr.get(startIndex));
newAns.add(newTemp);
}
combinations(arr,newAns, startIndex+1, endIndex, sum);
}
I have to rewrite your code as I was unable to think through your code.
Secondly, I have to make a newArrayList all time to avoid ConcurrentModificationException which I have faced the first time and will overcome later after gaining some knowledge about it.
Now, this method should be called as
public static void main (String[] args) {
ArrayList<Integer> arr = new ArrayList<Integer>();
arr.add(10);
arr.add(15);
arr.add(13);
arr.add(-5);
arr.add(28);
combinations(arr, new ArrayList<ArrayList<Integer>>(), 0, 4, 28);
}
Explanation: I have generalized your question's answer to fit any sum in int range. I have made ArrayList of ArrayList which will print all the combinations at the base case.
Upvotes: 1