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.

Results 1 to 12 of 12

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

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java algorithm to print all the combination of the integer array

    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2012
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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
    If you don't understand my answer, don't ignore it, ask a question.

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

    MrFesko (March 25th, 2013)

  6. #5
    Junior Member
    Join Date
    Jul 2012
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java algorithm to print all the combination of the integer array

    I donot understand.....

  7. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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!".
    If you don't understand my answer, don't ignore it, ask a question.

  8. #7
    Junior Member
    Join Date
    Jul 2012
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Java algorithm to print all the combination of the integer array

    Hi,

    Yes, is there any algorithm for it..

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Junior Member
    Join Date
    Jul 2012
    Posts
    20
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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. #10
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default 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.
    Writing code is your job, helping you fix and understand it is mine.

    <-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!

  12. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #12
    Junior Member
    Join Date
    Mar 2013
    Posts
    26
    Thanks
    2
    Thanked 2 Times in 2 Posts

    Default 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

Similar Threads

  1. How to convert String array to Integer array...?'
    By suyog53 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: September 25th, 2012, 08:50 AM
  2. Replies: 1
    Last Post: August 5th, 2012, 09:28 PM
  3. Replies: 8
    Last Post: April 14th, 2012, 12:24 PM
  4. High / low integer computation algorithm????
    By nakedtriple in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 22nd, 2011, 12:40 PM
  5. Need help deserializing an integer array.
    By jeremy_ in forum Collections and Generics
    Replies: 10
    Last Post: May 30th, 2011, 11:50 AM

Tags for this Thread