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