Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

calculate all possible sums from one int array

Thread view: 
jacksu - 23 Jun 2006 17:48 GMT
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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.