-2

I have the following code for subset sum which is suitable for integers. How to extend this code to double data type input? for example, how to extend this same code when the input is 1.01,2.65,3.08,4.07,5.12 (say) and output is 15.62 (say).These inputs and out are example even if they vary the code should work.

// A Java program to count all subsets with given sum.
import java.util.ArrayList;
public class subset_sum
{
// 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(arr[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(arr[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};
    int n = arr.length;
    int sum = 10;
    printAllSubsets(arr, n, sum);
}
}

Output:[4, 3, 2, 1] [5, 3, 2] [5, 4, 1]

Ajinkya
  • 1,797
  • 3
  • 24
  • 54
  • 2
    what has this question got to do with Python? – Scary Wombat Sep 04 '17 at 06:07
  • 5
    You cannot reasonably use `double` with this algorithm, but you could scale the inputs until they are integers. – harold Sep 04 '17 at 06:10
  • What is the complexity of this algorithm? Does it work with very large or wide-ranged integers (such as you would get when scaling up doubles)? – Thilo Sep 04 '17 at 06:12
  • @ScaryWombat I tried changing from int to double before posting the question here if you are aware this absolutely works, please let me know how? – Ajinkya Sep 04 '17 at 06:14
  • 1
    @Thilo it looks like the standard algorithm, so it would be pseudopolynomial, it doesn't like wide integers. His example input should be fine though. – harold Sep 04 '17 at 06:15
  • @Thilo Ajinkya - Sorry please disregard my previous comment - not enough analysis undertaken :-( – Scary Wombat Sep 04 '17 at 06:23
  • So after changing `int` to `double`, what happened? You don't say what your problem is. But here is a guess: `0.1 + 0.2` is not equal to `0.3`, so I'm closing this question with reference to "[Is floating point math broken?](https://stackoverflow.com/q/588004/5221149)". – Andreas Sep 04 '17 at 06:44
  • 1
    @Andreas I think that is not appropriate for this question, just because floating point is involved does not mean that it is a dupe of that famous question. A big part of the issue here is that we're doing DP with (if we do the obvious change) doubles as index into an array. – harold Sep 04 '17 at 06:52

1 Answers1

2

I found answer to this question by simply converting double to integer by calculating decimal places and multiply it by 100(say) as the algorithm uses addition this change does not affect final values in that case I divided the final value by 100 to get the result and displayed it in double data type

Ajinkya
  • 1,797
  • 3
  • 24
  • 54