The Find Array Given Subset Sums coding problem is a challenging mathematical puzzle. You are given an array sums containing all possible subset sums of an unknown array of length . Your task is to reconstruct the original array. The original array can contain negative integers.
This "Hard" difficulty problem is used by companies like Mindtickle to test a candidate's mastery of Divide and Conquer and combinatorial logic. Reconstructing a set from its subset sums requires a deep understanding of how the presence of a single element splits the set of sums into two halves: those that include and those that don't. It evaluation your ability to handle complex recursion and identify mathematical invariants.
This problem uses a recursive Divide and Conquer strategy.
sums array.sums[1] - sums[0]) is a candidate for an element in the original array.sums into two equal-sized arrays: S0 (sums not containing ) and S1 (sums containing ).
S0 or S1) contains the sum 0. If S0 has 0, then is part of the original array. If S1 has 0, then is part of the original array (and we continue with S1)., sums = [0, 1, 2, 3]
[0, 1, 2, 3].S0 = [0, 2], S1 = [1, 3].S0 contains 0, is an element.sums = [0, 2]. Difference .[1, 2].S1, it means the original element was negative.This problem is essentially the inverse of the subset sum problem. When dealing with power sets or subset sums, look for the smallest or largest gaps. These gaps often reveal the individual elements that were used to build the sum.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| XOR After Range Multiplication Queries II | Hard | Solve | |
| Median of Two Sorted Arrays | Hard | Solve | |
| Number of Ships in a Rectangle | Hard | Solve | |
| Beautiful Array | Medium | Solve | |
| Fill a Special Grid | Medium | Solve |