0

help, I made a code that allows to calculate the permutations of a number. for example if I enter the number 2090, this returns me the solutions:

2090, 0092, 0290, 0209, 9200, 9002, 9020, 0029, 0920, 0902, 2900, 2009, 2090, 0092, 0290, 0209, 9200, 9002, 9020, 0029, 0920, 0902, 2900, 2009.

then filter the solutions so that they are multiple of 11, have no zeros left and do not repeat. this filter gives me if I use the same number (2090) the following solutions:

2090 9020

here is where I have the problem, if I enter for example the number 2900 should give me the same solutions as when entering 2090, but I only return a solution that is 9020 How can I fix this error?

this is the code:

public static void main(String[] args) {

        ArrayList<String> list = new ArrayList<String>();
        Scanner read = new Scanner(System.in);

        System.out.println("Enter a Number");
        String n = read.next();

        //Calculate number of permutations
        long nPer = 1;
        for (int i = 1; i <= n.length(); i++) {
            nPer *= i;

        }
        System.out.println("Number of Permutations:" + nPer);
        char nums[] = n.toCharArray();

        //performs the permutation of the number according to the number of permutations
        for (int i = 0; i < nPer; i++) {

            /*
            valid if it is multiple of 11, does not have 0 to the left or if it is repeated and adds it to the ArrayList called list
             */

            if (Long.parseLong(String.valueOf(nums)) % 11 == 0) {
                if (nums[0] != '0') {
                    if (!list.contains(String.valueOf(nums))) {
                        list.add(String.valueOf(nums));
                    }
                }
            }

            char t = nums[i % (nums.length - 1)];
            nums[i % (nums.length - 1)] = nums[nums.length - 1];
            nums[nums.length - 1] = t;

        }
        //Display list with final solutions
        System.out.println("______________");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }
        System.out.println(list.size());

    }
Sanjeev
  • 9,876
  • 2
  • 22
  • 33

1 Answers1

0

Here is why posting mcve is so helpful:
When you eliminate the filters :

public static void main(String[] args) throws IOException {

    String n = "2900";
    ArrayList<String> list = new ArrayList<>();

    //Calculate number of permutations
    long nPer = 1;

    for (int i = 1; i <= n.length(); i++) {
        nPer *= i;
    }

    char nums[] = n.toCharArray();

    //performs the permutation of the number according to the number of permutations
    for (int i = 0; i < nPer; i++) {

        list.add(String.valueOf(nums));

        char t = nums[i % (nums.length - 1)];
        nums[i % (nums.length - 1)] = nums[nums.length - 1];
        nums[nums.length - 1] = t;
    }
    //Display list with final solutions

    System.out.println("______________");
    for (String s:  list) { System.out.println(s); }       
}

You get:

2900 0902 0209 0290 0290 0092 0029 9020 9020 9002 2009 2900 2900 0902 0209 0290 0290 0092 0029 9020 9020 9002 2009 2900

As you can see 2090 combination is not generated. On the other hands, other combinations appear more than once. So now it is very clear that you need to look into:

        char t = nums[i % (nums.length - 1)];
        nums[i % (nums.length - 1)] = nums[nums.length - 1];
        nums[nums.length - 1] = t;

(Always verify that a piece of code works as expected, before adding complexity)
To properly find permutations, try:

ArrayList<String> list = new ArrayList<>(permutation(n));

private static Set<String>  permutation(String s) {

    if((s == null) || (s.length() < 2) ) {
        return null;
    }

    Set<String> permutations = new HashSet<>();
    permutation(0,s,permutations);
    return permutations;
}

private static void  permutation(int index, String s, Set<String> permutations) {

    permutations.add(s);

    if(index >= (s.length()-1)) {
        return;
    }

    char[] chars = s.toCharArray();

    for(int pos=0; pos < s.length() ; pos++) {

        char[] temp  = s.toCharArray();

        char c = temp[pos];
        temp[pos] = chars[index];
        temp[index] = c;

        permutation(index +1, String.valueOf(temp),  permutations);
    }

}
c0der
  • 18,467
  • 6
  • 33
  • 65