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

  • SEARCH
  • Home
  • 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 6727943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:06:36+00:00 2026-05-26T10:06:36+00:00

I have this code…that does just about exactly what I need it to. It

  • 0

I have this code…that does just about exactly what I need it to. It searches a predefined array of ints for two ints that sum up to a target int. However, when putting values into the vector, rather than placing them within cells, it places all the values together.
i.e. for int array[50,40,30,20,10] and target 50, rather than returning [[50][40,10][30,20]…etc.], it prints [[50,40,10,30,20…etc.]] How can I fix this?

public Vector<Vector<Integer>> subsetSum(int[] array, int target) {
    //creates vectors, adds inner vector to another vector
    outer = new Vector<Vector<Integer>>();
    inner = new Vector<Integer>();
    outer.add(inner);

    for (int k = 0; k < array.length; k++) {
        for (int l = 1; l < array.length; l++) {
            int sum = array[k]+array[l]; //sum of l and k
            if (sum == target) {
                //add l,k to vector
                inner.add(array[l]);
                inner.add(array[k]);
                //prints l and k if their sum equals target
                System.out.println(array[l]+"+"+array[k]+"="+target);
            }
            else {
                System.out.print("");
            }
        }
        //if k is the target, display
        if (array[k] == target) {
            //add k to vector
            inner.add(array[k]);
            //prints if int equals target
            System.out.println(array[k]+"="+target);
        }
    }
    //return combinations that add up to target in vector form
    return outer;
}
  • 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-26T10:06:37+00:00Added an answer on May 26, 2026 at 10:06 am

    You’re only ever adding a single vector to outer. Isn’t it the case that if you find a pair that add up to the required sum, you want them in a distinct vector? So, you need to create a new “inner” vector when that happens, and add it to outer.

    Remove these lines:

    inner = new Vector<Integer>();
    outer.add(inner);
    

    Change:

    if (sum == target) {
        inner = new Vector<Integer>();
        outer.add(inner)
        //add l,k to vector
        inner.add(array[l]);
        inner.add(array[k]);
    

    And:

    if (array[k] == target) {
        inner = new Vector<Integer>();
        outer.add(inner)
        //add k to vector
        inner.add(array[k]);
    

    Finally, consider making inner and outer into local variables.

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

Sidebar

Related Questions

I have this code that sends just one attachment by time, how can I
I have this code that is supposed to take an array with some words,
I have this code in jQuery, that I want to reimplement with the prototype
I have this code that performs an ajax call and loads the results into
I have this code, that when swapping the order of UsingAs and UsingCast, their
I have this code to get a row from a JTable that is generated
I have this code (below) working just fine, however whenever I try to 'wrap'
I have this code that I call on each touch event that render an
I have this code, that draws an image. private void timer1_Tick(object sender, EventArgs e)
I have this code that activates when rollover, rollout, and release. I functions for

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.