-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathequal_average_partition.cpp
196 lines (196 loc) · 5.9 KB
/
equal_average_partition.cpp
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//Problem Link - https://www.interviewbit.com/problems/equal-average-partition/
/* Complet solution Code ------------------------+
vector<vector<vector<bool>>> dp; <--------------+
vector<int> res;
vector<int> original;
int total_size;
bool ispossible(int index, int current_sum, int current_size)
{
if(current_size == 0) return (current_sum == 0);
if(index >= total_size) return false;
if(dp[index][current_sum][current_size] == false) return false;
if(current_sum >= original[index])
{
res.push_back(original[index]);
if(ispossible(index+1, current_sum-original[index], current_size-1)) return true;
res.pop_back();
}
if(ispossible(index+1, current_sum, current_size)) return true;
return dp[index][current_sum][current_size] = false;
}
vector<vector<int>> equalaveragevector(vector<int> vec)
{
dp.clear(); original.clear();res.clear();
sort(vec.begin(), vec.end());
original = vec;
total_size = vec.size();
int total_sum=0;
for (int i = 0; i < vec.size(); i++)
{
total_sum += vec[i];
}
dp.resize(original.size(), vector<vector<bool>> (total_sum+1, vector<bool> (total_size, true)));
for (int i = 1; i < total_size; i++)
{
if((total_sum*i)%total_size != 0) continue;
int curr_sum = (i*total_sum)/total_size;
if(ispossible(0, curr_sum, i))
{
int ptr1=0, ptr2=0;
vector<int> res1 =res;
vector<int> res2;
while(ptr1 < vec.size() || ptr2 < res.size())
{
if(ptr2 < res.size() && res[ptr2] == original[ptr1])
{
ptr1++;
ptr2++;
continue;
}
res2.push_back(vec[ptr1]);
ptr1++;
}
vector<vector<int>> ans;
if(res2.size() > res1.size())
{
ans.push_back(res1);
ans.push_back(res2);
}
else if(res2.size() < res1.size())
{
ans.push_back(res2);
ans.push_back(res1);
}
else{
for (int k = 0; k < res1.size(); k++)
{
if(res1[k] > res2[k])
{
ans.push_back(res2);
ans.push_back(res1);
break;
}
else if(res1[k]<res2[k])
{
ans.push_back(res1);
ans.push_back(res2);
break;
}
continue;
}
}
return ans;
}
}
vector<vector<int>> ans;
return ans;
}
vector<vector<int> > Solution::avgset(vector<int> &A) {
vector<vector<int>> sol;
sol = equalaveragevector(A);
return sol;
}
*/
#include <bits/stdc++.h>
using namespace std;
vector<vector<vector<bool>>> dp;
vector<int> res;
vector<int> original;
int total_size;
bool ispossible(int index, int current_sum, int current_size)
{
if(current_size == 0) return (current_sum == 0);
if(index >= total_size) return false;
if(dp[index][current_sum][current_size] == false) return false;
if(current_sum >= original[index])
{
res.push_back(original[index]);
if(ispossible(index+1, current_sum-original[index], current_size-1)) return true;
res.pop_back();
}
if(ispossible(index+1, current_sum, current_size)) return true;
return dp[index][current_sum][current_size] = false;
}
vector<vector<int>> equalaveragevector(vector<int> vec)
{
dp.clear(); original.clear();res.clear();
sort(vec.begin(), vec.end());
original = vec;
total_size = vec.size();
int total_sum=0;
for (int i = 0; i < vec.size(); i++)
{
total_sum += vec[i];
}
dp.resize(original.size(), vector<vector<bool>> (total_sum+1, vector<bool> (total_size, true)));
for (int i = 1; i < total_size; i++)
{
if((total_sum*i)%total_size != 0) continue;
int curr_sum = (i*total_sum)/total_size;
if(ispossible(0, curr_sum, i))
{
int ptr1=0, ptr2=0;
vector<int> res1 =res;
vector<int> res2;
while(ptr1 < vec.size() || ptr2 < res.size())
{
if(ptr2<res.size() && res[ptr2] == original[ptr1])
{
ptr1++;
ptr2++;
continue;
}
res2.push_back(vec[ptr1]);
ptr1++;
}
vector<vector<int>> ans;
if(res2.size() > res1.size())
{
ans.push_back(res1);
ans.push_back(res2);
}
else if(res2.size() < res1.size())
{
ans.push_back(res2);
ans.push_back(res1);
}
else{
for (int k = 0; k < res1.size(); k++)
{
if(res1[k] > res2[k])
{
ans.push_back(res2);
ans.push_back(res1);
break;
}
else if(res1[k]<res2[k])
{
ans.push_back(res1);
ans.push_back(res2);
break;
}
continue;
}
}
return ans;
}
}
vector<vector<int>> ans;
return ans;
}
int main()
{
vector<int> A = {1,7,15,29,11,9};
vector<vector<int>> sol = equalaveragevector(A);
if(sol.empty()) return -1;
sort(sol.begin(), sol.end());
reverse(sol.begin(), sol.end());
for (int i = 0; i < sol.size(); i++)
{
for (int j = 0; j < sol[i].size(); j++)
{
cout<<sol[i][j]<<" ";
}
cout<<endl;
}
}