Find Array Given Subset Sums - Problem
The Mystery Array Detective Challenge

Imagine you're a data detective who needs to reconstruct a lost array from clues! You have an integer n representing the length of an unknown array, and you've been given an array sums containing all possible subset sums of this mystery array.

Here's the twist: the sums array contains exactly 2n values - one for each possible subset (including the empty subset with sum 0). Your mission is to reverse-engineer the original array from these subset sums.

Example: If the original array was [1, 3], the subset sums would be:
• Empty subset: 0
• Subset [1]: 1
• Subset [3]: 3
• Subset [1,3]: 4
So sums = [0, 1, 3, 4] (in any order)

Can you crack the code and find the original array?

Input & Output

example_1.py — Basic Case
$ Input: n = 3, sums = [0,0,5,5,4,-1,4,-1]
Output: [2, 3, -1]
💡 Note: The original array [2, 3, -1] generates these subset sums: {} = 0, {2} = 2 (not in sums as given), {3} = 3, {-1} = -1, {2,3} = 5, {2,-1} = 1 (not shown), {3,-1} = 2 (not shown), {2,3,-1} = 4. The sums array contains all these values with possible duplicates.
example_2.py — Simple Two Elements
$ Input: n = 2, sums = [0,2,3,5]
Output: [2, 3]
💡 Note: Array [2, 3] produces subset sums: {} = 0, {2} = 2, {3} = 3, {2,3} = 5. This perfectly matches the given sums array.
example_3.py — Single Element
$ Input: n = 1, sums = [0,7]
Output: [7]
💡 Note: With only one element, the subset sums are: {} = 0 and {7} = 7. So the original array must be [7].

Constraints

  • 1 ≤ n ≤ 15
  • sums.length == 2n
  • -104 ≤ sums[i] ≤ 104
  • Important: There is always exactly one valid solution

Visualization

Tap to expand
🔍 Reverse Engineering the Mystery ArrayMystery BoxOriginal Array[?, ?, ?]Given: All Subset Sums[0, 2, 3, 5, 1, 4, 6, 8]2³ = 8 total sumsfrom unknown 3-element arrayGoal: FindOriginal Array[1, 2, 3]Any valid solution🧩 Divide & Conquer StrategyStep 1Find minSmallest value = 1Must be array elementStep 2PartitionGroup A: [0,2,3,5]Group B: [1,3,4,6]Step 3RecurseSolve Group A→ returns [2,3]Step 4Combine[2,3] + [1]= [2,3,1]⚡ Time: O(n × 2ⁿ) | Space: O(2ⁿ)
Understanding the Visualization
1
Identify Key Ingredient
Find the cheapest single ingredient (smallest absolute sum) - this must be in our recipe
2
Separate Combinations
Split all combinations into those with/without our key ingredient
3
Recurse on Remainder
Apply the same process to find remaining ingredients
4
Combine Results
Build the complete recipe by combining all discovered ingredients
Key Takeaway
🎯 Key Insight: The smallest absolute non-zero subset sum must be an element of the original array. This allows us to partition and conquer recursively!
Asked in
Google 15 Meta 12 Microsoft 8 Amazon 6
28.4K Views
Medium Frequency
~25 min Avg. Time
847 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen