Magicsheet logo

Find Array Given Subset Sums

Hard
100%
Updated 6/1/2025

Asked by 1 Company

Find Array Given Subset Sums

What is this problem about?

The Find Array Given Subset Sums coding problem is a challenging mathematical puzzle. You are given an array sums containing all 2n2^n possible subset sums of an unknown array of length nn. Your task is to reconstruct the original array. The original array can contain negative integers.

Why is this asked in interviews?

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 xx splits the set of sums into two halves: those that include xx and those that don't. It evaluation your ability to handle complex recursion and identify mathematical invariants.

Algorithmic pattern used

This problem uses a recursive Divide and Conquer strategy.

  1. Sort the sums array.
  2. The smallest element difference (often sums[1] - sums[0]) is a candidate for an element xx in the original array.
  3. Split the sums into two equal-sized arrays: S0 (sums not containing xx) and S1 (sums containing xx).
    • This is done by iterating through the sorted sums and pairing up ss and s+xs+x.
  4. Check which of the two arrays (S0 or S1) contains the sum 0. If S0 has 0, then xx is part of the original array. If S1 has 0, then x-x is part of the original array (and we continue with S1).
  5. Recursively repeat the process with the chosen half until you have found nn elements.

Example explanation

n=2n = 2, sums = [0, 1, 2, 3]

  1. Sorted sums: [0, 1, 2, 3].
  2. Difference x=sums[1]sums[0]=10=1x = sums[1] - sums[0] = 1 - 0 = 1.
  3. Try splitting with x=1x = 1:
    • 00 pairs with 11.
    • 22 pairs with 33.
    • S0 = [0, 2], S1 = [1, 3].
  4. Since S0 contains 0, x=1x = 1 is an element.
  5. Repeat for sums = [0, 2]. Difference x=20=2x = 2 - 0 = 2.
  6. Final array: [1, 2].

Common mistakes candidates make

  • Not sorting: The pairings only work reliably if the sums are sorted.
  • Handling negative numbers: Failing to realize that if 0 ends up in the "shifted" array S1, it means the original element was negative.
  • Complexity: Attempting to use a brute-force combination check, which is O(2Nimes2N)O(2^N imes 2^N)—impossible for n=15n=15.

Interview preparation tip

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.

Similar Questions