0

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);
}

}

Waqas Umer
  • 99
  • 2
  • 13
  • 1
    Comments are not for extended discussion; this conversation has been [moved to chat](https://chat.stackoverflow.com/rooms/188734/discussion-on-question-by-umer-waqas-how-to-print-possible-permutations-of-the-a). If you are asked for additional information, please [edit] it into your question. – Cody Gray - on strike Feb 20 '19 at 08:27

2 Answers2

0

Take a look at example below:

public class Main {
    static int[] requestedNumbs = {3,4,6,2,1};


public static List<List<Integer>> permuteUnique(int[] nums, int index) {
        List<List<Integer>> newList = new LinkedList<>();
        if (index == nums.length - 1) {
            List<Integer> newPermute = new LinkedList<>();
            newPermute.add(nums[index]);
            newList.add(newPermute);
        } else {
            List<List<Integer>> oldList = permuteUnique(nums, index + 1);
            for (List<Integer> permute : oldList) {
                int permuteSize = permute.size();
                int i = 0;
                for (; i < permuteSize; i++) {
                    List<Integer> newPermute = new LinkedList<>();
                    newPermute.addAll(permute.subList(0, i));
                    newPermute.add(nums[index]);
                    newPermute.addAll(permute.subList(i, permuteSize));
                    newList.add(newPermute);
                    if (permute.get(i) == nums[index]) {
                        break;
                    }
                }
                if (i == permuteSize) {
                    List<Integer> newPermute = new LinkedList<>();
                    newPermute.addAll(permute.subList(0, permuteSize));
                    newPermute.add(nums[index]);
                    newList.add(newPermute);
                }
            }
        }
        return newList;
    }

    public static void main(String[] args) {
       List<List<Integer>> list = permuteUnique(requestedNumbs,0);
       System.out.println("Size of a list: " + list.size());
       System.out.println(list);
    }
}
MS90
  • 1,219
  • 1
  • 8
  • 17
  • I've try this example it's showing correct result but when i'm giving input in this form "3, 10, 4, 6, 1, 8, 5, 9, 0, 11, 7, 2". It do not show any output – Waqas Umer Feb 20 '19 at 08:27
  • Take a look at this https://stackoverflow.com/questions/1393486/error-java-lang-outofmemoryerror-gc-overhead-limit-exceeded – MS90 Feb 20 '19 at 09:45
0

If your task is just to calculate the number of possible permutations for the given array, you don't need to actually make and output those permutations. Notice that number grows exponentially. Instead, you can just calculate the number mathematically. Just do n! in a simple for loop.

If there really can be only unique integers:

public int permutations(int[] numbers){
  int result = 1;
  for(int i=2; i<=numbers.size(); i++){
    result*=i;
  }
  return result;
}
Ivan Kukic
  • 425
  • 4
  • 14