-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetAllPermutations.java
82 lines (74 loc) · 2.5 KB
/
GetAllPermutations.java
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import java.util.*;
public class GetAllPermutations {
//
// public static void main(String args[]) {
// int[] numbers = {1, 2, 3, 4};
// List<int[]> combinationx = new ArrayList<>();
// combinations(2, numbers, combinationx,2);
//
// for (int[] combination : combinationx) {
// System.out.println("x"+Arrays.toString(combination));
// }
// }
public static String GET(int pairOf, int[] numbers,int maxCount) {
String s="";
List<int[]> combinationx = new ArrayList<>();
combinations(pairOf, numbers, combinationx,maxCount);
for (int[] combination : combinationx) {
s+=Arrays.toString(combination)+"\n";
}
return s;
}
public static List<int[]> combinations(int n, int[] arr, List<int[]> list,int maxcount) {
// Calculate the number of arrays we should create
int numArrays = (int)Math.pow(arr.length, n);
// Create each array
for(int i = 0; i < numArrays; i++) {
list.add(new int[n]);
}
// Fill up the arrays
for(int j = 0; j < n; j++) {
// This is the period with which this position changes, i.e.
// a period of 5 means the value changes every 5th array
int period = (int) Math.pow(arr.length, n - j - 1);
for(int i = 0; i < numArrays; i++) {
int[] current = list.get(i);
// Get the correct item and set it
int index = i / period % arr.length;
current[j] = arr[index];
}
}
return removeDubler(list, maxcount);
}
public static List<int[]> removeDubler( List<int[]> list,int maxcount) {
ArrayList<int[]> indextoRemove=new ArrayList<>();
for (int[] is : list) {
if(maxcount(is)>maxcount) {
indextoRemove.add(is);
}
}
System.out.println(list.size());
for (int[] integer : indextoRemove) {
System.out.println(Arrays.toString(integer));
list.remove(integer);
}
System.out.println(list.size());
return list;
}
private static int maxcount(int[] array) {
int count=0;
int maxcount=0;
for (int i = 0; i < array.length; i++) {
count=0;
for (int j = 0; j < array.length; j++) {
if(array[i]==array[j]) {
count++;
}
}
if(count>=maxcount) {
maxcount=count;
}
}
return maxcount;
}
}