You are not logged in. Please login at to post your questions!


ANUMLA - Editorial




Author: Anudeep Nekkanti
Tester: Constantine Sokol
Editorialist: Florin Chirica




greedy, heaps (or STL multiset)


You have an unknown set of length N. We take all 2 ^ N subsets of it and sum elements for each subset. Given what we obtained, restore a possible initial set.


We build our solution step by step. Each step we take smallest element from sums. Suppose we’re at step i and we found element x[i]. We should erase now from sums all sums formed by x[i] and a non-empty subset of {x[1], x[2], ..., x[i – 1]}.


Let’s call all 2^N sums sumSet. Also, let’s call a possible solution valueSet (making sum of all subsets of valueSet, you should obtain sumSet). The problem says there is always a possible solution. We’ll implement sumSet as a multiset from C++. This container allows following things, which will be needed later: find/delete an element and keep the set in increasing order. We’ll note first element from current sum set as sumSet[1], second element as sumSet[2] and so on. Let’s read all numbers from the input and add all of them in multiset sumSet.

Smallest element from sumSet is always 0 (and it corresponds to empty subset). It does not give us any information, so let’s erase it from the set and move on. What’s smallest element now? Is it an element from valueSet? Is it a sum of a subset of valueSet?

There exists at least one element from valueSet equal to smallest element from sumSet. Why? Suppose first element of sumSet is a sum of other elements of valueSet. sumSet[1] = valueSet[k1] + valueSet[k2] + .... where k1, k2, ... are some indexes.

Since numbers are positive, we get that valueSet[k1] <= sumSet[1], valueSet[k2] <= sumSet[1] and so on. Since sumSet[1] is smallest element possible, we can only get that valueSet[k1] = sumSet[1], valueSet[k2] = sumSet[1] and so on.

This means at least one element from valueSet will have value equal to sumSet[1]. We’ve found one element from valueSet. Let’s add it to valueSet (we build the set incrementally) and erase it from sumSet. Let’s move now to our new sumSet[1] element (smallest element from sumSet, not deleted yet). We can follow same logic from above and see that sumSet[1] is a new element from valueSet. Let’s add it to valueSet and erase it from sumSet.

We move once again to sumSet[1]. Now, we have a problem. It can be one of following 2 possibilities:

  • sum of subset {valueSet[1], valueSet[2]}

  • a new element of valueSet.

Case b) is ideal, because we found one more element of valueSet. What to do with case a)? We know sum valueSet[1] + valueSet[2]. So we can simply erase it from sumSet, before considering sumSet[1]. Then, only case a) is left, so we find valueSet[3]. We erase now valueSet[3] from sumSet (I know, it becomes boring already, I’ll finish in a moment :) ).

It’s more tricky now what can be sumSet[1]. It can be one of following: valueSet[3]+valueSet[1], valueSet[3]+valueSet[2], valueSet[3]+valueSet[1]+valueSet[2]. We can fix this by erasing all those elements from sumSet before considering sumSet[1]. Once again, we’re left with valueSet[4]. Let’s note that all sums that should be erased contain a valueSet[3] term and a non-empty subset of {valueSet[1], valueSet[2]}. Sums of subsets of {valueSet[1], valueSet[2]} are already erased in previous steps.

Generalizing the algorithm

Let’s generalize the algorithm. Suppose you want to calculate valueSet[n]. We need firstly to erase from set a combination of valueSet[n – 1] and a non-empty subset of {valueSet[1], valueSet[2], ..., valueSet[n – 2]}. Then, the smallest element is valueSet[n].

We can keep an additional array subsets[] representing all subset sums obtained from {valueSet[1], valueSet[2], ..., valueSet[n – 2]}. Then, at step of calculating valueSet[n], we need to erase subsets[j] + valueSet[n – 1] from our sumSet. Now, valueSet[n] is calculated.

The new subset sum list will be the old one plus the one that contains valueSet[n – 1]. So, after we calculate valueSet[n], we update subsets with all values valueSet[n – 1] + subSets[j]. We run this algorithm as long as there is at least one element in sumSet.

Time Complexity

Each element is added in the multiset once and erased once. Hence, the complexity is O(2 ^ N * log(2 ^ N)) = O(2 ^ N * N).


Author's solution
Tester's solution

This question is marked "community wiki".

asked 20 Oct '14, 00:22

elfus's gravatar image

3★elfus ♦♦
accept rate: 0%

edited 20 Oct '14, 03:25

dpraveen's gravatar image

4★dpraveen ♦♦

please add solutions...

(20 Oct '14, 00:34) rudra_sarraf3★

Solutions are not opening, check the links please

(20 Oct '14, 00:45) saurabhsuniljain2★

it's ok now ;-)

(20 Oct '14, 01:01) betlista ♦♦3★

very well written :)

(20 Oct '14, 02:05) kunalkukreja4★

Very nice explanation :)

(20 Oct '14, 02:20) aj954★

The concept is quite nice. I tried to implement exactly the same thing in code. However, I did not know about the container, and hence my solution became very complicated. :(

(20 Oct '14, 10:38) paramjitrohit2★
showing 5 of 6 show all

1234next »

The valueSet and sumSet thing is not very understandable.


answered 20 Oct '14, 01:10

rick93's gravatar image

accept rate: 0%

Weak test cases for this question.... My logic is completely wrong but it passed in the practice section. one of the test case is 1 , 3 , 0 1 1 2 2 3 3 4 giving 1 1 3 but correct answer is 1 1 2 .

sol link :


answered 21 Oct '14, 07:55

hatim009's gravatar image

accept rate: 8%

(21 Oct '14, 08:21) hatim0093★

here in PREREQUISITES it says heap.. what is the use of heap here ??


answered 05 Oct '17, 16:18

bhola_nit's gravatar image

accept rate: 0%

I have written an editorial about this problem here.


answered 10 Jun, 15:03

mathprogrammer's gravatar image

accept rate: 0%

5193943 is my submission id. I haven`t been able to find counter test case. So kindly to my solution which is wrong is given. Thanks :)


answered 20 Oct '14, 01:30

yogeshkr0007's gravatar image

accept rate: 0%

edited 20 Oct '14, 02:56


Hey @yogeshkr0007 have a look this testcase is the one to which your solution is wrong

(20 Oct '14, 02:33) hkbharath3★

hey. i edited the link. same situ though. thanks anyways.

(20 Oct '14, 02:57) yogeshkr00073★

May you please tell me how you got 0 0 1 1 1 1 2 2 by adding the elements of subsets of set={0,1,0} in your given test case as the possible subsets are {},{0},{1},{0,1},{1,0},{0},{0,1,0},{0,0} and the array formed is 0 0 1 1 1 0 1 0.Correct me if I am wrong.

(20 Oct '14, 19:38) rishavz_sagar3★

@hkbharath The test case you provided is wrong and does not satisfies the constraints in the first place. It is clearly give that the original array can only consist of positive integers, hence you cannot have more than one 0's in the test case itself.

(20 Oct '14, 20:25) ayushj103★

@rishavz_sagar 0,1,0 is the wrong answer for input 0 0 1 1 1 1 2 2. I gave a testcase for which code was returning wrong answer.

(28 Aug '17, 15:03) hkbharath3★

I implemented a similar algorithm using unordered_map. I belive that the amortized time-complexity must be around the same. Could anyone help me understanding why this would give TLE? Here is the relevant link. Thanks.


answered 20 Oct '14, 04:51

fool_for_cs's gravatar image

accept rate: 33%

There can be problem when we have the next element in the valueset that is equal to sum of other subsets in valueset. for example, when we have our original valueset as 1 1 2. Then, the sumset would have originally contain 0 1 1 2 2 3 3 4. So, after finding Valuset[0] = 1, Valuset[1] = 1. If we remove ValueSet[0] + ValueSet[1] i.e. 2 from SumSet, then it would remove the original ValueSet[2] = 2 element. And the new state of the Sumset would be 3 3 4. So, we will miss element 2.

Please clariy if i am wrong anywhere.


answered 20 Oct '14, 12:35

suraj_singh89's gravatar image

accept rate: 0%

Why are you removing both 2's??It's a multi-set, so only one instance of 2 will be removed and then it will become {2,3,3,4}.

(20 Oct '14, 14:31) xpertcoder4★

Amazing !!! Anyone please look at these two solutions : 1.) 2.) .First one is TLE whereas second is AC. Onlyone difference is that i'm just using "st.count" for checking whether that element is in that multiset before deleting it, it's a natural way of deleting element from STL containers..But,it gave TLE, whereas not checking this gives AC . Anyone please explain me the behavior or uses of these functions.


answered 20 Oct '14, 15:39

thecoderju_np's gravatar image

accept rate: 0%

That is because count() is linear in time.Refer this link I guess overall complexity is increased to O((2^n)log(2^n)(number of subsets at that time)).

(20 Oct '14, 21:28) xpertcoder4★

answered 20 Oct '14, 16:38

biprotip's gravatar image

accept rate: 0%

Kudos to your Explanation and your Code Anudeep


answered 20 Oct '14, 17:51

raviteja1452's gravatar image

accept rate: 0%

toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text]( "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:


question asked: 20 Oct '14, 00:22

question was seen: 12,105 times

last updated: 10 Jun, 15:03