Problem Description
Given an integer n representing the length of an unknown array and an array sums containing the 2^n subset sums (in no particular order) of the unknown array, recover any valid original array which could have produced these subset sums. The empty subset is included (with sum 0). If multiple answers exist, any one is acceptable.
Key Insights
- The sorted list of subset sums always starts with 0.
- The second smallest element in the sorted subset sums is the smallest element in the unknown array.
- Once an element is identified, the subset sums that include this element can be “removed” from the multiset of sums.
- A multiset (or counter) is used to keep track of frequencies of current sums so that duplicates are properly handled.
- Repeating this process iteratively recovers all n elements of the unknown array.
Space and Time Complexity
Time Complexity: O(n * 2^n) in the worst case, since for each of the n recovered elements we process a subset of size roughly 2^n. Space Complexity: O(2^n) for storing the multiset (or counter) of subset sums.
Solution
The main idea is to iteratively determine one element of the original array at a time. Start by sorting the subset sums. The smallest element (0) represents the empty subset, and the next smallest value is the smallest element in the original array. Once this candidate element is identified, generate all new subset sums by adding this element to every previously found subset sum (starting with just 0). Remove these newly generated sums from the existing multiset of subset sums. This “removal” effectively partitions the sums into those that include the candidate and those that do not. Then, repeat the process for the remaining elements until all n elements have been recovered. Data structures used include a multiset (or counter/dictionary) to track frequency and a list to hold the current subset sums built from found elements.