简体   繁体   中英

Return all possible sums that can be formed by using numbers in array JAVA

For some reason I am getting the right answers, but they are being duplicated, and I am not sure why. Any ideas?

Here's my code so far:

import java.util.ArrayList;

public class Problem2 
{

public static void getSum(int[] numbersArray, int starting, int sum)
{
    if(numbersArray.length == starting)
    {   
        return;
    }
    int value = sum + numbersArray[starting];

    getSum(numbersArray, starting + 1, value);
    getSum(numbersArray, starting + 1, sum);

    System.out.print(sum + " " + value + " ");
}

public static void main(String[] args)
{
    getSum(new int[] {3, 5}, 0, 0);
}
}

I think that your logic is slightly incorrect when you print results in getSum method.

I changed getSum method and now it works as expected:

public static void getSum(int[] numbersArray, int starting, int sum)
{
    if(numbersArray.length == starting)
    {
        // Now we print sum here
        System.out.println(sum);
        return;
    }

    int value = sum + numbersArray[starting];

    getSum(numbersArray, starting + 1, value);
    getSum(numbersArray, starting + 1, sum);
}

For 3, 5 it gives 8 3 5 0 .

For 1, 2, 4, 5 it gives 12 7 8 3 10 5 6 1 11 6 7 2 9 4 5 0 .

Your code works (it evaluates all the sums by chosing all possible combinations of elements from the input array), but with that aproach you can't eliminate duplicates.

Here's the solution with Set collection (collection that stores only unique values):

public class Problem2 
{
    public static Set getSum(int[] numbersArray, int starting)
    {
        if(numbersArray.length == starting)
        {   
            return new HashSet();
        }

        Set recursiveSet = getSum(numbersArray, starting + 1);

        Set newSet = new HashSet();

        int value = numbersArray[starting];

        for(Object object : recursiveSet) {
          int element = (int) object;

          newSet.add(element);
          newSet.add(element + value);
        }

        newSet.add(value);

        return newSet;
    }

    public static void main(String[] args)
    {
        Set set = getSum(new int[] {3, 5}, 0);

        for(Object object : set) {
          int element = (int) object;
          System.out.println(element);
        }
    }
}

So, the idea is that when you compute all the sums of numersArray from starting position "i+1" to the end (labeled "recursiveSet" above), you could store all that sums in a new Set (labeled "newSet" above), and then also add to that set numbersArray[i] + sum, where the sum is any value from the previous Set ("recursiveSet").

You only need to print the "leaves" of your "recursive tree", using a similar approach. The duplicates you have are the printings done at upper levels in the sequence of recursive calls when you're arrived at the end of the array and "move back" returning. Just move the printing statement in the if clause that check when you're arrived at the end of the array, before returning, and you're done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM