Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4253244
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:55:13+00:00 2026-05-21T04:55:13+00:00

Does anyone know how to implement the Sum-of-Subsets problem in Java from this pseudo

  • 0

Does anyone know how to implement the Sum-of-Subsets problem in Java from this pseudo code?

w = an array of positive integers sorted in non-decreasing order.
W = the target sum value
include = an array or arraylist of the solutions who's weight adds up to W. After the print statement, this array can be deleted so that it can store the next solution.
weight = weight of elements in the include array.
total = weight of the remaining elements not in the include array.

public static void sum_of_subsets(index i, int weight, int total)
{
     if(promising(i))
     {
          if(weight == W)
          {
               System.out.print(include[1] through include[i]);
          }
          else
          {
               include[i + 1] = "yes";     //Include w[i + 1]
               sum_of)subsets(i + 1, weight + w[i + 1], total - w[i + 1]);
               include[i + 1] = "no";      //Do not include w[i + 1]
               sum_of_subsets(i + 1, weight, total - w[i + 1]);
          }
     }
}

public static boolean promising(index i);
{
     return (weight + total >= W) && (weight == W || weight + w[i + 1] <= W);
}

this is really baffling me, so if you could add comments that would be great!!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T04:55:13+00:00Added an answer on May 21, 2026 at 4:55 am

    Algorithms Coded in Java

    First the algorithm removes all numbers that are larger than the sum to begin with.

    Then for the largest number smaller than the sum, it checks if there are any numbers in the list that it can add to itself to get the sum. Once we have either found a pair, or the sum is greater than the desired sum, we can break since the list is sorted. We then consider the second largest number and see if we can make a pair with that, and so on.

       /**
        * This will find how many pairs of numbers in the given array sum
        * up to the given number.
        *
        * @param array - array of integers
        * @param sum - The sum
        * @return int - number of pairs.
        */
    public static int sumOfSubset(int[] array, int sum)
    {
            // This has a complexity of O ( n lg n )
            Arrays.sort(array);
    
            int pairCount = 0;
            int leftIndex = 0;
            int rightIndex = array.length - 1;
    
            // The portion below has a complextiy of
            //  O ( n ) in the worst case.
            while (array[rightIndex] > sum + array[0])
            {
                rightIndex--;    
            }
    
            while (leftIndex < rightIndex)
            {
                if (array[leftIndex] + array[rightIndex] == sum)
                {
                    pairCount++;
                    leftIndex++;
                    rightIndex--;
                }
                else if(array[leftIndex] + array[rightIndex]  < sum)
                {
                    leftIndex++;
                }
                else
                {
                    rightIndex--;   
                }
            }
    
            return pairCount;
    }
    

    The algorithm above does not return the pairs, but that is trivially to add.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how to implement the algorithm for this problem using the Knapsack
Does anyone know how to implement the standard bubble message that warns users whenever
does anyone know is there a way to implement Windows Live ID authentication into
Does anyone know a good Java lib that will hook into SVN so I
Does anyone know how to implement an action badge, like the ones seen below
Does anyone know how to implement a LOV (List Of Values) search in an
Does anyone know how to implement a view similar to the iphone calendar day
Does anyone know of any reference or sample code of the Minimum Error Thresholding
Does anyone know of any attempt to implement the Web Sockets API using Boost
Does anyone know if it is possible to implement TTS in iOS development like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.