• Post Reply Bookmark Topic Watch Topic
  • New Topic

Permutations in Java  RSS feed

 
Rahul Dayal Sharma
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have 5 numbers between 0 to 13 in ascending order stored in an array. They may have repeats. I want to perform permutations and figure out the number of running pairs (minimum 3 numbers in a pair. Ex- 012, 345, 456, 1234, 78910) that I can make with these command line inputs that I get.
Lets say my user input was 01123. For this sequence I'll have the following pairs-

012
012
1234
1234
123
123
0123
0123

There are same repeated sequences because there are two 1's hence same pairs using different 1's.

I have the input in an array so I'm wondering if there is any inbuilt class for arrays that would let me do this. If not, whether I can pass it into a collection and then find its permutations. Following which I traverse through the sequences and update a count for each running pair generated to pass onto an array in another variable to perform some other functionality. If this is viable can you suggest me how to implement this in code ? I'm new to java and am not familiar with the classes available.

Any and all help will be highly appreciated
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know of any existing classes that handle this easily - I think you'll have to code it yourself. Whether you use an array or a List is a relatively minor point; getting the code correct will be the more important part. To get started, I would suggest that based on your example, what you want is to generate all possible combinations with a minimum number of elements. Not permutations. If you were counting permutations, then you should also list 021, 102, 120, 201, 210, etc. If you ignore order, as you are doing, then you're dealing with combinations rather than permutations.
 
Rahul Dayal Sharma
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

sorry about that. someone suggested me to get the permutations and weed out the combinations without a running sequence. My bad.

Can you suggest some code or logic to tackle this issue if I just want to formulate these combinations ?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!