Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

Members have full access to the forums. Advertisements are removed for registered users.

# Thread: Java algorithm to print all the combination of the integer array

1. ## Java algorithm to print all the combination of the integer array

Hi,

I have an integer array and i want to print all the combinations of this integer array. can you please suggest me the algorithm?

e.g int n[] ={1,2,3}
and its combinations are 123,132,231,213,312,321

problem is that i want the general algorithm which is independent of the array size.

3. ## Re: Java algorithm to print all the combination of the integer array

Hi ,

I try and find the algorithm but its acutal combinations are less than expectation.
e.g if i have 6 number then my code gives 126 combinations .
i try other algorithm but it repeats the combinations
so please suggest the link for algorithm for this ........

4. ## Re: Java algorithm to print all the combination of the integer array

Would this do it:
Use all digits one at a time in first position
use remaining digits one at a time in next position
etc until no digits left

5. ## The Following User Says Thank You to Norm For This Useful Post:

MrFesko (March 25th, 2013)

6. ## Re: Java algorithm to print all the combination of the integer array

I donot understand.....

7. ## Re: Java algorithm to print all the combination of the integer array

I think you are asking for:
number of permutations of n distinct objects is n×(n − 1)×(n − 2)×⋯×2×1, which is commonly denoted as "n factorial" and written "n!".

8. ## Re: Java algorithm to print all the combination of the integer array

Hi,

Yes, is there any algorithm for it..

9. ## Re: Java algorithm to print all the combination of the integer array

What did google say? It usually has algorithms for common problems like this one.

10. ## Re: Java algorithm to print all the combination of the integer array

hi,

i found some algorithm but they miss some combinations...

is there any algorithm that print all the combinations and exactly once?

11. ## Re: Java algorithm to print all the combination of the integer array

Sounds like you'll have to write the algorithm yourself. Take some time and think about it. If you run into road blocks come ask for help. I could just write the algorithm and give it to you, but that wont help you learn, you need to give it a shot.

12. ## Re: Java algorithm to print all the combination of the integer array

Are you searching for combinations or permutations?
This looks like permutations: 123,132,231,213,312,321

What code have you tried?

13. ## Re: Java algorithm to print all the combination of the integer array

It seems as if it is a matter of the length of the array. Ignoring the possibility of two of the same number in the array, this should work:

```public int combinations(int length){
if(length == 1){
return 1;
}
else{
return length*combinations(length-1);
}
}```

The parameter for this method can be the length of the array. For example, n.length