Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: A Challenge Coding problem! Java coding for combinational sum

  1. #1
    Junior Member
    Join Date
    Jun 2021
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default A Challenge Coding problem! Java coding for combinational sum

    I got this java code for combinational sum:
    *****************************
    JavaScript:
    // Java program to find all combinations that
    // sum to a given value
    import java.io.*;
    import java.util.*;

    class GFG {

    static ArrayList<ArrayList<Integer> >
    combinationSum(ArrayList<Integer> arr, int sum)
    {
    ArrayList<ArrayList<Integer> > ans
    = new ArrayList<>();
    ArrayList<Integer> temp = new ArrayList<>();

    // first do hashing since hashset does not always
    // sort
    // removing the duplicates using HashSet and
    // Sorting the arrayList

    Set<Integer> set = new HashSet<>(arr);
    arr.clear();
    arr.addAll(set);
    Collections.sort(arr);

    findNumbers(ans, arr, sum, 0, temp);
    return ans;
    }

    static void
    findNumbers(ArrayList<ArrayList<Integer> > ans,
    ArrayList<Integer> arr, int sum, int index,
    ArrayList<Integer> temp)
    {

    if (sum == 0) {

    // Adding deep copy of list to ans

    ans.add(new ArrayList<>(temp));
    return;
    }

    for (int i = index; i < arr.size(); i++) {

    // checking that sum does not become negative

    if ((sum - arr.get(i)) >= 0) {

    // adding element which can contribute to
    // sum

    temp.add(arr.get(i));

    findNumbers(ans, arr, sum - arr.get(i), i,
    temp);

    // removing element from list (backtracking)
    temp.remove(arr.get(i));
    }
    }
    }

    // Driver Code

    public static void main(String[] args)
    {
    ArrayList<Integer> arr = new ArrayList<>();

    arr.add(900);
    arr.add(800);
    arr.add(700);
    arr.add(600);
    arr.add(500);
    arr.add(100);
    arr.add(50);
    arr.add(1);
    int sum = 2000;

    ArrayList<ArrayList<Integer> > ans
    = combinationSum(arr, sum);

    // If result is empty, then
    if (ans.size() == 0) {
    System.out.println("Empty");
    return;
    }

    // print all combinations stored in ans

    for (int i = 0; i < ans.size(); i++) {

    System.out.print("(");
    for (int j = 0; j < ans.get(i).size(); j++) {
    System.out.print(ans.get(i).get(j) + " ");
    }
    System.out.print(") ");
    }
    }
    }
    **************************************
    what if I have large numbers like 900, 800, 700, 600, 500, 90, 80, 70, 60, 50, 9, 8, 7, 6, 5, 4, 3, 2, 1. and I want the combination of numbers to sum to 2000, how man listed combination. There would be many ones to list, how to limit the pick of 1s or 2s elements if it is going to be too many to list?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,147
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: A Challenge Coding problem! Java coding for combinational sum

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. HW problem. Trying to finish up the coding for this assignment
    By hodnee in forum What's Wrong With My Code?
    Replies: 9
    Last Post: February 12th, 2014, 03:28 AM
  2. hangman problem java coding
    By angelshark in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 8th, 2013, 12:55 PM
  3. Help with Java coding problem!
    By eyesackery in forum Java Theory & Questions
    Replies: 4
    Last Post: June 4th, 2012, 08:21 AM
  4. Coding Problem
    By BohmfalkCW in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 26th, 2012, 06:10 PM
  5. Replies: 2
    Last Post: February 19th, 2012, 07:36 AM