I'm working on task in which i have to show the possible permutations of the length of array. I've tried some tricks but it's still giving the list of many outputs, and in the end my project get crash.
I've tried mathematically and i got answer "39916800.
For example: For input array [3,2,1,4,6], there are totally 5! = 120 possible.
The question is:
Given that int [] a = [3, 10, 4, 6, 1, 8, 5, 9, 0, 11, 7, 2]. How often do you have to permute a with yourself until you get a again (this is the so-called degree of a)?
Example: The degree of [0,2,1] is 2, because permute ([0,2,1], [0,2,1]) = [0,1,2] and permute ([0,1, 2], [0,2,1]) = [0,2,1].
The answer should be 8 digits.
Here is my code:
public class Permute{
static void permute(java.util.List<Integer> arr, int k){
for(int i = k; i < arr.size(); i++){
java.util.Collections.swap(arr, i, k);
permute(arr, k+1);
java.util.Collections.swap(arr, k, i);
}
if (k == arr.size() -1){
System.out.println(java.util.Arrays.toString(arr.toArray()));
}
}
public static void main(String[] args){
Permute.permute(java.util.Arrays.asList(3,4,6,2,1), 0);
}
}