Reputation: 3110
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;
}
Upvotes: 0
Views: 344
Reputation: 20631
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.
Upvotes: 3
Reputation: 6047
Deepa's answer is half-way there, but you'll also need to create a new instance of inner
inside the for
loop before adding the values to it; otherwise, you'll end up with all the values inside of one inner
. Like this:
final Vector<Integer> inner = new Vector<Integer>();
outer.add(inner);
You can also continue
after adding to improve performance a bit.
Upvotes: 0
Reputation: 2494
Also, are you intending to have a vector of vectors? because at the moment, you have a vector, with one vector named inner in it, so everything is being added straight into inner. You are not creating a new vector each time to put each pair in.
Upvotes: 0
Reputation: 8590
Move these two lines:
inner = new Vector<Integer>();
outer.add(inner);
into the outer loop (the one with index variable k
.)
Upvotes: 0
Reputation: 2494
I think your mistake lies in add vector value in outer at very first so its return all the value from outer vector only not in the inner vector.. have to added after checking condition
if (array[k] == target) {
//add k to vector
inner.add(array[k]);
//prints if int equals target
System.out.println(array[k]+"="+target);
}
outer.add(inner)
Upvotes: 0