-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathPermutationNoDuplicatesDemo.java
More file actions
40 lines (33 loc) · 962 Bytes
/
PermutationNoDuplicatesDemo.java
File metadata and controls
40 lines (33 loc) · 962 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package ds_008_recursion;
public class PermutationNoDuplicatesDemo {
public static void main(String[] args) {
int numberOfChoices = 3;
int[] permutations = new int[2];
makePermutation(numberOfChoices, permutations);
}
// Helper method
private static void makePermutation(int numberOfChoices, int[] permutations) {
boolean used[] = new boolean[numberOfChoices+1];
makePermutation(0, permutations, used);
}
private static void makePermutation(int index, int[] permutations, boolean[] used) {
if(index == permutations.length) {
printArray(permutations);
} else {
for(int i = 1; i < used.length; i++) {
if(!used[i]) {
used[i] = true;
permutations[index] = i;
makePermutation(index + 1, permutations, used);
used[i] = false;
}
}
}
}
private static void printArray(int[] array) {
for(int i = 0; i < array.length; i++) {
System.out.printf("%d ", array[i]);
}
System.out.print("\n");
}
}