Skip to main content
Question Protected by CommunityBot
edited title; edited tags; fixed i.e. → e.g.
Source Link
200_success
  • 145.7k
  • 22
  • 191
  • 481

Given a target sum, populate Find all subsets, whose sum is equal to the targer sum, from of an int array whose sums equal a given target

i.e.For example:

Given a target sum, populate all subsets, whose sum is equal to the targer sum, from an int array

i.e.

Find all subsets of an int array whose sums equal a given target

For example:

added 352 characters in body; edited tags; edited tags; edited tags
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Function ==> Given a target sum, populate all subsets, whose sum is equal to the targer sum, from an int array.

Given a target sum, populate all subsets, whose sum is equal to the target sum, from an int array.

An intint array is { 1, 3, 4, 5, 6, 15 }{ 1, 3, 4, 5, 6, 15 }.

I am using java.util.Stackjava.util.Stack class to implement this function, along with recursionrecursion.

My code is as follows:

GetAllSubsetByStack class ==> class

import java.util.Stack;

public class GetAllSubsetByStack {

    /** Set a value for target sum */
    public static final int TARGET_SUM = 15;

    private Stack<Integer> stack = new Stack<Integer>();

    /** Store the sum of current elements stored in stack */
    private int sumInStack = 0;

    public void populateSubset(int[] data, int fromIndex, int endIndex) {

        /*
        * Check if sum of elements stored in Stack is equal to the expected
        * target sum.
        * 
        * If so, call print method to print the candidate satisfied result.
        */
        if (sumInStack == TARGET_SUM) {
            print(stack);
        }

        for (int currentIndex = fromIndex; currentIndex < endIndex; currentIndex++) {

            if (sumInStack + data[currentIndex] <= TARGET_SUM) {
                stack.push(data[currentIndex]);
                sumInStack += data[currentIndex];

                /*
                * Make the currentIndex +1, and then use recursion to proceed
                * further.
                */
                populateSubset(data, currentIndex + 1, endIndex);
                sumInStack -= (Integer) stack.pop();
            }
 
        }
 
    }

    /**
    * Print satisfied result. i.e. 15 = 4+6+5
    */ 

    private void print(Stack<Integer> stack) {
        StringBuilder sb = new StringBuilder();
        sb.append(TARGET_SUM).append(" = ");
        for (Integer i : stack) {
            sb.append(i).append("+");
        }
        System.out.println(sb.deleteCharAt(sb.length() - 1).toString());
    }
}

Main class ==>

public class Main {

    private static final int[] DATA = { 1, 3, 4, 5, 6, 2, 7, 8, 9, 10, 11, 13,
        14, 15 };

    public static void main(String[] args) {
        GetAllSubsetByStack get = new GetAllSubsetByStack();
        get.populateSubset(DATA, 0, DATA.length);
    }
}

Could you pleasePlease help me with the following 2 things:

THING#1 ==> How can I improve this code to reduce the times for resursion? Is sorting the int array (from high to low) before resursion a better way?

THING#2 ==> Is there a way to improve the code without using recursion?

  1. How can I improve this code to reduce the times for recursion? Is sorting the int array (from high to low) before recursion a better way?

  2. Is there a way to improve the code without using recursion?

Function ==> Given a target sum, populate all subsets, whose sum is equal to the targer sum, from an int array.

An int array is { 1, 3, 4, 5, 6, 15 }.

I am using java.util.Stack class to implement this function, along with recursion.

My code is as follows:

GetAllSubsetByStack class ==>

import java.util.Stack;

public class GetAllSubsetByStack {

/** Set a value for target sum */
public static final int TARGET_SUM = 15;

private Stack<Integer> stack = new Stack<Integer>();

/** Store the sum of current elements stored in stack */
private int sumInStack = 0;

public void populateSubset(int[] data, int fromIndex, int endIndex) {

    /*
     * Check if sum of elements stored in Stack is equal to the expected
     * target sum.
     * 
     * If so, call print method to print the candidate satisfied result.
     */
    if (sumInStack == TARGET_SUM) {
        print(stack);
    }

    for (int currentIndex = fromIndex; currentIndex < endIndex; currentIndex++) {

        if (sumInStack + data[currentIndex] <= TARGET_SUM) {
            stack.push(data[currentIndex]);
            sumInStack += data[currentIndex];

            /*
             * Make the currentIndex +1, and then use recursion to proceed
             * further.
             */
            populateSubset(data, currentIndex + 1, endIndex);
            sumInStack -= (Integer) stack.pop();
        }
 
    }
 
}

/**
 * Print satisfied result. i.e. 15 = 4+6+5
 */
private void print(Stack<Integer> stack) {
    StringBuilder sb = new StringBuilder();
    sb.append(TARGET_SUM).append(" = ");
    for (Integer i : stack) {
        sb.append(i).append("+");
    }
    System.out.println(sb.deleteCharAt(sb.length() - 1).toString());
}
}

Main class ==>

public class Main {

private static final int[] DATA = { 1, 3, 4, 5, 6, 2, 7, 8, 9, 10, 11, 13,
        14, 15 };

public static void main(String[] args) {
    GetAllSubsetByStack get = new GetAllSubsetByStack();
    get.populateSubset(DATA, 0, DATA.length);
}
}

Could you please help me with the following 2 things:

THING#1 ==> How can I improve this code to reduce the times for resursion? Is sorting the int array (from high to low) before resursion a better way?

THING#2 ==> Is there a way to improve the code without using recursion?

Given a target sum, populate all subsets, whose sum is equal to the target sum, from an int array.

An int array is { 1, 3, 4, 5, 6, 15 }.

I am using java.util.Stack class to implement this function, along with recursion.

GetAllSubsetByStack class

import java.util.Stack;

public class GetAllSubsetByStack {

    /** Set a value for target sum */
    public static final int TARGET_SUM = 15;

    private Stack<Integer> stack = new Stack<Integer>();

    /** Store the sum of current elements stored in stack */
    private int sumInStack = 0;

    public void populateSubset(int[] data, int fromIndex, int endIndex) {

        /*
        * Check if sum of elements stored in Stack is equal to the expected
        * target sum.
        * 
        * If so, call print method to print the candidate satisfied result.
        */
        if (sumInStack == TARGET_SUM) {
            print(stack);
        }

        for (int currentIndex = fromIndex; currentIndex < endIndex; currentIndex++) {

            if (sumInStack + data[currentIndex] <= TARGET_SUM) {
                stack.push(data[currentIndex]);
                sumInStack += data[currentIndex];

                /*
                * Make the currentIndex +1, and then use recursion to proceed
                * further.
                */
                populateSubset(data, currentIndex + 1, endIndex);
                sumInStack -= (Integer) stack.pop();
            }
        }
    }

    /**
    * Print satisfied result. i.e. 15 = 4+6+5
    */ 

    private void print(Stack<Integer> stack) {
        StringBuilder sb = new StringBuilder();
        sb.append(TARGET_SUM).append(" = ");
        for (Integer i : stack) {
            sb.append(i).append("+");
        }
        System.out.println(sb.deleteCharAt(sb.length() - 1).toString());
    }
}

Main class

public class Main {

    private static final int[] DATA = { 1, 3, 4, 5, 6, 2, 7, 8, 9, 10, 11, 13,
        14, 15 };

    public static void main(String[] args) {
        GetAllSubsetByStack get = new GetAllSubsetByStack();
        get.populateSubset(DATA, 0, DATA.length);
    }
}

Please help me with the following 2 things:

  1. How can I improve this code to reduce the times for recursion? Is sorting the int array (from high to low) before recursion a better way?

  2. Is there a way to improve the code without using recursion?

Tweeted twitter.com/#!/StackCodeReview/status/405733235155808256
deleted 2 characters in body; edited tags
Source Link
Malachi
  • 29.1k
  • 11
  • 87
  • 188
Source Link
Mengjun
  • 403
  • 1
  • 4
  • 6
Loading