calculate all possible sums from one int array

J

jacksu

say I have one int array {3, 5, 3, 7}

want to get
{3, 5, 7, 8, 6, 10, 15, 13, 16, 18}

I thought this could be very common issue, anyone has algorithm on
that?

Thanks.
 
T

Thomas Kellerer

jacksu wrote on 23.06.2006 18:48:
say I have one int array {3, 5, 3, 7}

want to get
{3, 5, 7, 8, 6, 10, 15, 13, 16, 18}

I thought this could be very common issue, anyone has algorithm on
that?

Why do you think this should be a "common" issue? I can't think of any
real-world application that would need something like that - but then maybe I
just don't know enough parts of the "real world" ;)

Thomas
 
V

vjg

jacksu said:
say I have one int array {3, 5, 3, 7}

want to get
{3, 5, 7, 8, 6, 10, 15, 13, 16, 18}

I thought this could be very common issue, anyone has algorithm on
that?

Thanks.

public class IntArray {

public static void main(String[] args) {
int[] i = {3,5,3,7};
i = new int[] {3,5,7,8,6,10,15,13,16,18};
}
}
 
G

Gijs Peek

vjg said:
say I have one int array {3, 5, 3, 7}

want to get
{3, 5, 7, 8, 6, 10, 15, 13, 16, 18}

I thought this could be very common issue, anyone has algorithm on
that?

Thanks.

public class IntArray {

public static void main(String[] args) {
int[] i = {3,5,3,7};
i = new int[] {3,5,7,8,6,10,15,13,16,18};
}
}

I think this would be explained in your book section called "backtracking"
 
L

lordy

say I have one int array {3, 5, 3, 7}

want to get
{3, 5, 7, 8, 6, 10, 15, 13, 16, 18}

I thought this could be very common issue, anyone has algorithm on
that?

Very common - for coursework.

Lordy
 
M

Mike Schilling

Thomas Kellerer said:
jacksu wrote on 23.06.2006 18:48:

Why do you think this should be a "common" issue? I can't think of any
real-world application that would need something like that - but then
maybe I just don't know enough parts of the "real world" ;)

It's common in many parts of the world: high schools, colleges,
universities, ...
 
B

Barry

jacksu said:
say I have one int array {3, 5, 3, 7}

want to get
{3, 5, 7, 8, 6, 10, 15, 13, 16, 18}

I thought this could be very common issue, anyone has algorithm on
that?

Thanks.
Hint: Set<Integer>
 
S

Scott Ellsworth

jacksu said:
say I have one int array {3, 5, 3, 7}

want to get
{3, 5, 7, 8, 6, 10, 15, 13, 16, 18}

I thought this could be very common issue, anyone has algorithm on
that?

It is quite common in coursework, and is usually done when finding the
'combinations' of a data set.

Out of your combinations algorithm, you will get, most likely, a set of
indices of your starting array, listing the indices in that particular
combination.

e.g.

1
1 2
1 3
1 4
1 2 3
1 2 4
1 3 4
1 2 3 4
2
2 3
2 4
2 3 4
3
3 4
4

Take each combination, extract the ints for those indices from the
source array.

e.g.

3
3 5
3 3
3 7
3 5 3
3 5 7
3 3 7
3 5 3 7
5
5 3
5 7
5 3 7
3
3 7
7

Sum them.

If the result is not in your output array, add it. You might find the
Integer class and a Set<Integer> useful at this point to strip out
duplicates.

If you use a TreeSet<Integer>, they even come out sorted.

Scott
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top