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.
Thomas Kellerer - 23 Jun 2006 18:01 GMT
jacksu wrote on 23.06.2006 18:48:
> say I have one int array {3, 5, 3, 7}
>
[quoted text clipped - 3 lines]
> 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
Mike Schilling - 23 Jun 2006 22:40 GMT
> jacksu wrote on 23.06.2006 18:48:
>> say I have one int array {3, 5, 3, 7}
[quoted text clipped - 8 lines]
> 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, ...
vjg - 23 Jun 2006 18:45 GMT
> say I have one int array {3, 5, 3, 7}
>
[quoted text clipped - 5 lines]
>
> 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};
}
}
Gijs Peek - 23 Jun 2006 21:07 GMT
>> say I have one int array {3, 5, 3, 7}
>>
[quoted text clipped - 13 lines]
> }
> }
I think this would be explained in your book section called "backtracking"
lordy - 23 Jun 2006 22:01 GMT
> say I have one int array {3, 5, 3, 7}
>
[quoted text clipped - 3 lines]
> I thought this could be very common issue, anyone has algorithm on
> that?
Very common - for coursework.
Lordy
Barry - 23 Jun 2006 22:46 GMT
> say I have one int array {3, 5, 3, 7}
>
[quoted text clipped - 5 lines]
>
> Thanks.
Hint: Set<Integer>
Scott Ellsworth - 23 Jun 2006 23:27 GMT
> say I have one int array {3, 5, 3, 7}
>
[quoted text clipped - 3 lines]
> 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

Signature
Scott Ellsworth
scott@alodar.nospam.com
Java and database consulting for the life sciences