-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathequalAvgPart.java
More file actions
67 lines (49 loc) · 1.8 KB
/
equalAvgPart.java
File metadata and controls
67 lines (49 loc) · 1.8 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
public class equalAvgPart {
/*Given an array with non negative numbers, divide the array into two parts such that the average of both the
* parts is equal. Return both parts (If exist). If there is no solution. return an empty list. * */
static double avgT = 0;
static boolean T[][][];
ArrayList<Integer> res, orig;
static int totalSize;
public boolean subsetSum(int a[], int total) {
//Find if any subset sums up to total
boolean T[][] = new boolean[a.length + 1][total + 1];
for (int i = 0; i <= a.length; i++) {
T[i][0] = true;
}
for (int i = 1; i <= a.length; i++) {
for (int j = 1; j <= total; j++) {
if (j - a[i - 1] >= 0) {
T[i][j] = T[i - 1][j] || T[i - 1][j - a[i - 1]];
} else {
T[i][j] = T[i-1][j];
}
}
}
return T[a.length][total];
}
public ArrayList<ArrayList<Integer>> avgset(ArrayList<Integer> a) {
ArrayList<ArrayList<Integer>> arrayList = new ArrayList<>();
totalSize = a.size();
Collections.sort(a);
int totalSum = a.stream().mapToInt(Integer::intValue).sum();
avgT = totalSum / (double)totalSize;
T = new boolean[totalSize][totalSum + 1][totalSize];
orig = a;
res = new ArrayList<>();
for (int i = 1; i < totalSize; i ++){
if ((totalSum * i) % totalSize != 0)
continue;
int sumSetA = (totalSum * i) / totalSize;
}
return arrayList;
}
public static void main(String[] args) {
System.out.println(new equalAvgPart().avgset(new ArrayList<>(Arrays.asList(1, 7, 15, 29 , 11, 9))));
int t[] = {1, 7, 15, 29, 11, 9};
System.out.println(new equalAvgPart().subsetSum(t, 13));
}
}