-
Notifications
You must be signed in to change notification settings - Fork 1
/
find_array_given_subset.cpp
45 lines (42 loc) · 1.03 KB
/
find_array_given_subset.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
#include<bits/stdc++.h>
using namespace std;
class Solution
{
public:
vector<int> recoverArray(int n, vector<int> &sums)
{
sort(sums.begin(), sums.end());
vector<int> ans;
while (n--)
{
int diff = sums[1] - sums[0];
unordered_map<int, int> freq;
vector<int> ss0, ss1;
bool on = false;
for (auto &x : sums)
if (!freq[x])
{
ss0.push_back(x);
freq[x + diff]++;
if (x == 0)
on = true;
}
else
{
ss1.push_back(x);
freq[x]--;
}
if (on)
{
ans.push_back(diff);
sums = ss0;
}
else
{
ans.push_back(-diff);
sums = ss1;
}
}
return ans;
}
};