Question

Write a Java program, that given an array of ints and a desired target sum returns...

Write a Java program, that given an array of ints and a desired target sum returns the set of
combinations of any length that add up to that target sum.
Each set of combinations must be specified by the indexes (0-based) of the integers. See
examples below.
The code snippet below is pseudo-code and not Java.
EXAMPLE 1:
==============
total_combinations = calculate_combinations(input=[5, 5, 15, 10], target_sum=15)
ANSWER 1:
==============
should return 3 sets, as there are 3 combinations of numbers from the input array that add up
to 15, namely:
[2]           => input[2] = 15
[0, 3]       => input[0] = 5, input[3] = 10, sum = 15
[1, 3]       => input[1] = 5, input[3] = 10, sum = 15

EXAMPLE 2:
==============
total_combinations = calculate_combinations(input=[1, 2, 3, 4, 5, 6], target_sum=6)

ANSWER 2:
==============
should return 2 sets, as there are 2 combinations of numbers from the input array that add up
to 6, namely:
[0, 1, 2]
[1, 3]
[5]

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the code below::

SubSet_sum_problem.java

package classes3;
import java.util.ArrayList;
import java.util.Arrays;
public class SubSet_sum_problem
{
   // dp[i][j] is going to store true if sum j is
   // possible with array elements from 0 to i.
   static boolean[][] dp;

   static void display(ArrayList<Integer> v)
   {
       System.out.println(v);
   }

   // A recursive function to print all subsets with the
   // help of dp[][]. Vector p[] stores current subset.
   static void printSubsetsRec(int arr[], int i, int sum,
           ArrayList<Integer> p)
   {
       // If we reached end and sum is non-zero. We print
       // p[] only if arr[0] is equal to sun OR dp[0][sum]
       // is true.
       if (i == 0 && sum != 0 && dp[0][sum])
       {
           p.add(i);
           display(p);
           p.clear();
           return;
       }

       // If sum becomes 0
       if (i == 0 && sum == 0)
       {
           display(p);
           p.clear();
           return;
       }

       // If given sum can be achieved after ignoring
       // current element.
       if (dp[i-1][sum])
       {
           // Create a new vector to store path
           ArrayList<Integer> b = new ArrayList<>();
           b.addAll(p);
           printSubsetsRec(arr, i-1, sum, b);
       }

       // If given sum can be achieved after considering
       // current element.
       if (sum >= arr[i] && dp[i-1][sum-arr[i]])
       {
           p.add(i);
           printSubsetsRec(arr, i-1, sum-arr[i], p);
       }
   }

   // Prints all subsets of arr[0..n-1] with sum 0.
   static void printAllSubsets(int arr[], int n, int sum)
   {
       if (n == 0 || sum < 0)
           return;

       // Sum 0 can always be achieved with 0 elements
       dp = new boolean[n][sum + 1];
       for (int i=0; i<n; ++i)
       {
           dp[i][0] = true;   
       }

       // Sum arr[0] can be achieved with single element
       if (arr[0] <= sum)
           dp[0][arr[0]] = true;

       // Fill rest of the entries in dp[][]
       for (int i = 1; i < n; ++i)
           for (int j = 0; j < sum + 1; ++j)
               dp[i][j] = (arr[i] <= j) ? (dp[i-1][j] ||
                       dp[i-1][j-arr[i]])
                       : dp[i - 1][j];
               if (dp[n-1][sum] == false)
               {
                   System.out.println("There are no subsets with" +
                           " sum "+ sum);
                   return;
               }

               // Now recursively traverse dp[][] to find all
               // paths from dp[n-1][sum]
               ArrayList<Integer> p = new ArrayList<>();
               printSubsetsRec(arr, n-1, sum, p);
   }

   //Driver Program to test above functions
   public static void main(String args[])
   {
       int arr[] = {1, 2, 3, 4, 5, 6};
       int n = arr.length;
       int sum = 6;
       System.out.println("Array : "+Arrays.toString(arr));
       System.out.println("Target : "+sum);
      
       System.out.println("Combinations are as below");
       printAllSubsets(arr, n, sum);
   }
}

Add a comment
Know the answer?
Add Answer to:
Write a Java program, that given an array of ints and a desired target sum returns...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT