-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAllPermutations.java
More file actions
49 lines (42 loc) · 1.54 KB
/
AllPermutations.java
File metadata and controls
49 lines (42 loc) · 1.54 KB
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
41
42
43
44
45
46
47
48
49
import java.util.ArrayList;
import java.util.List;
public class AllPermutations {
// Function to generate all permutations
public static void generate(int nums[], List<Integer> ds, List<List<Integer>> ans, boolean freq[]) {
if (ds.size() == nums.length) {
ans.add(new ArrayList<>(ds));
return;
}
for (int i = 0; i < nums.length; i++) {
if (!freq[i]) { // If the element has not been used
freq[i] = true;
ds.add(nums[i]);
generate(nums, ds, ans, freq);
ds.remove(ds.size() - 1); // Backtrack
freq[i] = false; // Mark the element as not used
}
}
}
// Function to return all permutations of the array
public static List<List<Integer>> all_permutations(int nums[]) {
List<List<Integer>> ans = new ArrayList<>();
List<Integer> ds = new ArrayList<>();
boolean[] freq = new boolean[nums.length]; // Boolean array to track used elements
if (nums.length == 0) {
return ans;
}
generate(nums, ds, ans, freq);
return ans;
}
public static void main(String[] args) {
int nums[] = {1, 2, 3};
List<List<Integer>> ans = all_permutations(nums);
// Print all permutations
for (int i = 0; i < ans.size(); i++) {
for (int j = 0; j < ans.get(i).size(); j++) {
System.out.print(ans.get(i).get(j) + " ");
}
System.out.println();
}
}
}