Public Group

# Recursive Algorithm Help

This topic is 2238 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi everyone,

I am trying to write a recurisve algoritm that, when given a list of numbers, checks to see if there is any combination of the numbers that will add up to a target value.

I'm attempting to write it using tail recursion.

The method works occasionally, but does not work every time. My problem is if I have a list {9, 7, 2, 5, 1} and the target is 11, obviously 2 and 9 will add together to become 11. BUT the way I have it written, it will add 1 + 5 (making 6). The next iteration will add the 2, making 8. After that, it will see the 7 and 9 and decide that the group does not exist. I do not see how to code it so that it can detect the 2 and 9 combination. Did that make sense?

[source lang=java]
public static boolean groupExists(final List<Integer> numbers, final int target)
{
if (numbers == null || numbers.isEmpty())
{
return false;
}
else if (target == 0)
{
return true;
}
else if (numbers.size() == 1)
{
return (numbers.get(0) == target);
}
else if (target == numbers.get(numbers.size()-1))
{
return true;
}
else
{
if (target > numbers.get(numbers.size()-1))
{
return groupExists(numbers.subList(0, numbers.size()-1), target - (numbers.size()-1));
}
else
{
return groupExists(numbers.subList(0, numbers.size()-1), target);
}
}
}
[/source]

I do not see how to get the method to check ALL possible combinations.

Can anyone give me some advice? Any help will be appreciated.

Thank you.

##### Share on other sites
The function doesn't seem to be doing any additions anywhere. Don't you want to check if the sum of two numbers equal to target?

##### Share on other sites
Your first case is wrongly placed: if numbers is empty, the method should still return true if target == 0.
Your third case (numbers.size() == 1) is a bit ugly: it is not really necessary, as it should be taken care of by the rest of the code.
The last case (else) seems to be wrong: to find a solution, you should both try out picking the last number and skipping it. You only check the skip path if the last number is greater than the target.

This would be the code in Haskell; you'll need to translate it into java. My algorithm is not tail recursive, but it is trivial to make it so by just returning a bool instead of the integer selection.

[source lang="plain"]sat :: Integer -> [Integer] -> Maybe [Integer]
----------------------------------------------
sat 0 _ = Just []
sat _ [] = Nothing
sat n (x:xs) = if n < 0
then Nothing
else do solution <- sat (n - x) xs
return (x : solution)
mplus
sat n xs[/source]
The last case (sat n (x:xs)) should coincide with your last else-branch. My algorithm works on the first item of the list instead of the last as you do. As you can see, first it tries picking the first value (sat (n - x) xs), and if that doesn't work, it tries skipping it (sat n xs).

##### Share on other sites
A simple non-recursive brute force method:

If you were to assign the inclusion of every member in the list as a bit in an integer: 5 numbers == 11111, gives you 32 combinations;

Then simply try out every option:

 for(int i = 0; i < 32; ++i){ int total = 0; //use list-value when bit is set for(int j = 0; j < 5; ++j) if(i & (1 << j)) if((total += list[j]) == target) return(true); } return(false); 

##### Share on other sites
Your problem looks like the famous algorithmic problem "Coin change". You could google around for solutions and make small modifications to fit your exact problem.

##### Share on other sites
Thank you for the replies.

Here is my updated program:
[source lang=java]
public static boolean groupExists(final List<Integer> numbers, final int target)
{
if (numbers == null)
{
return false;
}
else if (target == 0)
{
return true;
}
else if (numbers.size() == 0)
{
if (target == 0)
{
return true;
}
else
{
return false;
}
}
else if (target == numbers.get(numbers.size()-1))
{
return true;
}
else
{
if (target > numbers.get(numbers.size()-1))
{
return groupExists(numbers.subList(0, numbers.size()-1), target - numbers.get(numbers.size()-1));
}
else
{
if (numbers.size() > 3)
{
return groupExists(numbers.subList(0, numbers.size()-2), target);
}
else
{
return groupExists(numbers.subList(0, numbers.size()-1), target);
}
}
}
}
[/source]

It passes every single test I have written for it (about 20) except for one:
assertTrue(Exercises.groupExists(list, 31));

The variable list is defined: list = java.util.Arrays.asList(2, 7, 13, 9, 11);

Does anyone have any advice on how to change this so that the test does not fail?

Thank you

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 13
• 11
• 15
• 11
• 12
• ### Forum Statistics

• Total Topics
634151
• Total Posts
3015839
×