# Thread: How many possible groups in a serial of number, I need all of the goups

1. Member
Join Date
Jul 2012
Posts
3
Rep Power
0

## How many possible groups in a serial of number, I need all of the goups

Hi,
Just don't know how whether it can be written a program in Java that performs such a logic. Seems very complicated because I need all of the possible groups

Here's a simple example.
1 2 3 4 5 6 7

in these seven numbers

I just choose four of them. Can I write a jave program to list out all of the possible groups if I choose four of them?
For example
1 2 3 4 can be a group
2 3 5 6 also can be a group
......
I need to list out all of the possible groups and they should be order from small to large.
If I didn't do a wrong calculation, it may be 840 possible groups totally.

I have no idea about how to write such a program to fulfill such a logic in Java. Is it possible???
Thanks.

2. ## Re: How many possible groups in a serial of number, I need all of the goups

Is, say, 1 2 3 4 different from 4 3 2 1? (the same numbers but in a different order).

kind regards,

Jos

3. Member
Join Date
Jul 2012
Posts
3
Rep Power
0

## Re: How many possible groups in a serial of number, I need all of the goups

They are the same. Thanks for your kindly remind. So there maybe less than 840 possible groups since some of them are just in different order.

for example,
1 2 3 4
4 3 2 1
2 1 3 4
are the same. the result should be 1 2 3 4, I want them to be ordered.

Thanks.

Originally Posted by JosAH
Is, say, 1 2 3 4 different from 4 3 2 1? (the same numbers but in a different order).

kind regards,

Jos

4. ## Re: How many possible groups in a serial of number, I need all of the goups

So you want all C(n,m) combinations? (in your example C(7,4) == 7!/(4!*3!) == 35); have a look at the following method; given a 'group' stored in array 'c', the method generates the next group, given the size 'n' of the elements to pick from:

Java Code:
```	public static boolean combine(int[] c, int n) {

for (int i= c.length; --i >= 0;) {
if (++c[i] <= n-(c.length-i)) {
for (; ++i < c.length; )
c[i]= c[i-1]+1;
return true;
}
}
return false;
}```
In your example, create a group 0, 1, 2, 3 and a size n= 7 and run the above method over and over again until it returns false; each time it ran it has generated a next 'group'.

kind regards,

Jos

5. Member
Join Date
Jul 2012
Posts
3
Rep Power
0

## Re: How many possible groups in a serial of number, I need all of the goups

So greate that your mathematics is quite good. Your calculation is right and that's what I want.
I'll investigate the code and give you a reply if the program is OK.
Many thanks~

Originally Posted by JosAH
So you want all C(n,m) combinations? (in your example C(7,4) == 7!/(4!*3!) == 35); have a look at the following method; given a 'group' stored in array 'c', the method generates the next group, given the size 'n' of the elements to pick from:

Java Code:
```	public static boolean combine(int[] c, int n) {

for (int i= c.length; --i >= 0;) {
if (++c[i] <= n-(c.length-i)) {
for (; ++i < c.length; )
c[i]= c[i-1]+1;
return true;
}
}
return false;
}```
In your example, create a group 0, 1, 2, 3 and a size n= 7 and run the above method over and over again until it returns false; each time it ran it has generated a next 'group'.

kind regards,

Jos

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•