You are given an integer n representing the length of an unknown array that you are trying to recover. You are also given an array sums containing the values of all 2nsubset sums of the unknown array (in no particular order).
Return the arrayansof lengthnrepresenting the unknown array.
Ifmultiple answers exist, return any of them.
An array sub is a subset of an array arr if sub can be obtained from
arr by deleting some (possibly zero or all) elements of arr. The sum of the elements in sub is one possible subset sum of arr. The sum of an empty array is considered to be 0.
Note: Test cases are generated such that there will always be at least one correct answer.
Input: n =3, sums =[-3,-2,-1,0,0,1,2,3]Output: [1,2,-3]Explanation:[1,2,-3]is able to achieve the given subset sums:-[]: sum is0-[1]: sum is1-[2]: sum is2-[1,2]: sum is3-[-3]: sum is-3-[1,-3]: sum is-2-[2,-3]: sum is-1-[1,2,-3]: sum is0Note that any permutation of [1,2,-3] and also any permutation of [-1,-2,3] will also be accepted.
The smallest subset sum is always 0 (empty set). The next smallest subset sum must be the smallest element in the array (or its negative if the array contains negatives). We can recursively extract the smallest element, partition the subset sums into those that include it and those that do not, and repeat for the rest of the array.
classSolution {
public: vector<int> recoverArray(int n, vector<int>& sums) {
multiset<int> ms(sums.begin(), sums.end());
vector<int> ans;
while (n--) {
int x =*next(ms.begin());
int d = x -*ms.begin();
multiset<int> left, right;
map<int, int> cnt;
for (int v : ms) cnt[v]++;
for (int v : ms) {
if (cnt[v] ==0) continue;
left.insert(v);
cnt[v]--;
cnt[v + d]--;
}
if (left.size() < ms.size() /2) d =-d;
ans.push_back(d);
multiset<int> nextms;
cnt.clear();
for (int v : ms) cnt[v]++;
for (int v : ms) {
if (cnt[v] ==0) continue;
cnt[v]--;
cnt[v + d]--;
nextms.insert(v);
}
ms = nextms;
}
return ans;
}
};
classSolution {
funrecoverArray(n: Int, sums: IntArray): IntArray {
sums.sort()
val ans = mutableListOf<Int>()
var m = n
var s = sums.toList()
while (m-- > 0) {
val d = s[1] - s[0]
val cnt = mutableMapOf<Int, Int>()
for (v in s) cnt[v] = cnt.getOrDefault(v, 0) + 1val left = mutableListOf<Int>()
for (v in s) {
if (cnt[v] ==0) continue left.add(v)
cnt[v] = cnt[v]!! - 1 cnt[v + d] = cnt.getOrDefault(v + d, 0) - 1 }
val reald = if (left.size < s.size / 2) -d else d
ans.add(reald)
val cnt2 = mutableMapOf<Int, Int>()
for (v in s) cnt2[v] = cnt2.getOrDefault(v, 0) + 1val nexts = mutableListOf<Int>()
for (v in s) {
if (cnt2[v] ==0) continue cnt2[v] = cnt2[v]!! - 1 cnt2[v + reald] = cnt2.getOrDefault(v + reald, 0) - 1 nexts.add(v)
}
s = nexts
}
return ans.toIntArray()
}
}
classSolution:
defrecoverArray(self, n: int, sums: list[int]) -> list[int]:
from collections import Counter
sums.sort()
ans = []
for _ in range(n):
d = sums[1] - sums[0]
cnt = Counter(sums)
left = []
for v in sums:
if cnt[v] ==0:
continue left.append(v)
cnt[v] -=1 cnt[v + d] -=1if len(left) < len(sums) //2:
d =-d
ans.append(d)
cnt = Counter(sums)
nextsums = []
for v in sums:
if cnt[v] ==0:
continue cnt[v] -=1 cnt[v + d] -=1 nextsums.append(v)
sums = nextsums
return ans