Reconstruct Array from Subset Sums
Company: Goldman Sachs
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Other
You are given an integer `n`, the length of an unknown integer array, and an array `sums` that holds all ${2^n}$ subset sums of that array in no particular order. Every subset counts, including the empty subset (sum 0), and a sum appears as many times as there are subsets that produce it.
Return an array `ans` of length `n` whose subset sums are exactly `sums` as a multiset. If several arrays work, any one of them is accepted, with its elements in any order. Every input has at least one answer.
```python
def recover_array(n: int, sums: list[int]) -> list[int]:
...
```
Example 1:
```text
Input: n = 3, sums = [-3,-2,-1,0,0,1,2,3]
Output: [-3,1,2]
```
The subsets of [-3,1,2] are {}, {-3}, {1}, {2}, {-3,1}, {-3,2}, {1,2} and {-3,1,2}, with sums 0, -3, 1, 2, -2, -1, 3 and 0. That is the given multiset. The array [-2,-1,3] produces the same sums, so it is also accepted.
Example 2:
```text
Input: n = 2, sums = [0,0,0,0]
Output: [0,0]
```
Every subset of [0,0] sums to 0.
```hint Two smallest sums
Sort the sums. The smallest one is the sum of all the negative elements. What single change to that subset gives the second smallest sum?
```
```hint Peel one element at a time
Once you know the absolute value d of one element, the sums pair up as x and x + d. One of the two halves is the subset-sum multiset of the other elements. The empty subset tells you which half you can keep.
```
```hint Zeros and repeats
A zero element makes d equal 0, and repeated sums are normal. Match values by count, not by set membership.
```
### Constraints & Assumptions
- `1 <= n <= 15`
- `sums.length == 2^n`
- `-10^4 <= sums[i] <= 10^4`
- At least one valid array exists for every input.
- Any valid array is accepted, and its elements may be returned in any order.
### What a Strong Answer Covers
- How to read the absolute value of one element off the sorted sums, with a reason it works.
- How to split the multiset into the sums without that element and the sums with it, including repeated values and zero elements.
- How to choose the element's sign, and why the half that is kept is still the subset-sum multiset of a real array one element shorter.
- Time and space complexity in terms of ${2^n}$.
- A trace on a small input that shows the sign decision.
### Follow-up Questions
- How would you check that a returned array is correct, and what does the check cost?
- How does the algorithm simplify if every element is known to be non-negative?
- How many different valid arrays can one input have, and how would you describe all of them?
Overview: Given n and the multiset of all 2^n subset sums of an unknown integer array, recover an array that produces exactly those sums. It tests multiset reasoning, peeling off one element at a time, choosing signs when the array has negative values or zeros, and complexity analysis over exponential input.