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

• March 15th, 2013, 01:41 AM
ashish12169
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.
• March 15th, 2013, 06:18 AM
Norm
Re: Java algorithm to print all the combination of the integer array
• March 16th, 2013, 09:11 AM
ashish12169
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
• March 16th, 2013, 09:15 AM
Norm
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
• April 6th, 2013, 03:47 PM
ashish12169
Re: Java algorithm to print all the combination of the integer array
I donot understand.....
• April 6th, 2013, 04:30 PM
Norm
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!".
• April 8th, 2013, 08:03 AM
ashish12169
Re: Java algorithm to print all the combination of the integer array
Hi,

Yes, is there any algorithm for it..
• April 8th, 2013, 08:43 AM
Norm
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.
• April 10th, 2013, 01:49 PM
ashish12169
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?
• April 10th, 2013, 01:57 PM
Chris.Brown.SPE
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.
• April 10th, 2013, 01:58 PM
Norm
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?
• May 2nd, 2013, 05:55 PM
beansnbacon
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:

Code :

```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