ANUMLA - Editorial

PROBLEM LINK:

Practice
Contest

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

DIFFICULTY:

Easy

PREREQUISITES:

greedy, heaps (or STL multiset)

PROBLEM:

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.

QUICK EXPLANATION

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*. We should erase now from sums all sums formed by x* and a non-empty subset of {x[1], x[2], …, x[i – 1]}.

EXPLANATION

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 :slight_smile: ).

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 AND TESTER’S SOLUTIONS:

Author’s solution
Tester’s solution

43 Likes

The valueSet and sumSet thing is not very understandable.

6 Likes

5193943 is my submission id.
I haven`t been able to find counter test case.
So kindly help.link to my solution which is wrong is given.
http://www.codechef.com/viewsolution/5193943
Thanks :slight_smile:

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.
http://www.codechef.com/viewsolution/5184582

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.

Amazing !!!
Anyone please look at these two solutions :
1.) http://www.codechef.com/viewsolution/5195961
2.) http://www.codechef.com/viewsolution/5195984
.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.

1 Like

Kudos to your Explanation and your Code Anudeep

1 Like

Since all the elements of the array are positive integers. Can we sort the subsets and get the first smallest N numbers ? have I understood the problem correctly ?

Can anyone tell me what is wrong with following approach?

First check if there are more than 1 zeros. If there are two zeros then ‘0’ is the first element
otherwise if only one zero is found then the smallest non-zero positive element of given array is the first element of our ans.

Now subtract this min element from all other elements of given array. Again find the smallest positive and non-zero element it is the 2nd element and so on…

Please correct me if I am wrong!

Thank you!

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 : http://www.codechef.com/viewsolution/5196910

3 Likes

Can anyone provide me a good corner case where my solution fails? I’m getting WA.

Link to solution: http://www.codechef.com/viewsolution/5191980

Thanks in advance :slight_smile:

Can anyone give me a tricky/corner test case for this question.
I am getting WA.

can anyone explain the algorithm with example???

Can anyone please tell why the code is giving RTE http://ideone.com/sQbNRz

Can we apply the following algorithm?
The maximum element (say max) in the sumset will be equal to sum of all elements in required array (since all elements are +ve). we know the value of N. Then subtracting max from next N largest element will give us the value of N elements in a array…
P.S correct me if i am wrong

http://ideone.com/YSYAst ! for all test cases given above this gives the right answer … can anyone reckon me the test case where i am wrong?

sum of the test cases i worked on are :-

8

3

0 1 1 1 2 2 2 3

3

0 1 2 3 3 4 5 6

3

0 1 100 1000 101 1001 1100 1101

4

0 1 2 3 4 3 4 5 5 6 7 6 7 8 9 10

4

0 1 1 2 2 2 3 3 3 3 4 4 4 5 5 6

2

1000000000 1000000000 2000000000 0

1

0 21

2

0 0 0 0

Why s.erase(s.begin()) is not written in else part of the author’s solution?? Can anyone please explain?!!

Why my logic is not working (getting runtime error.)…

Please help me out…

logic :- As we have the size of array (which is lost) then numbers present apart from the first one (which is always 0) the numbers from index (1) <…as index 0 the number is zero…> to N (size of lost array) we can have the numbers lost…

For eg.

line 1 :1

line 2 :3

line 3 :0 1 2 3 3 4 5 6

Ans :(1,2,3)(…after that the numbers would the sums of these numbers…)

My solution is : http://www.codechef.com/viewsolution/7481486

Can anyone tell me why i am getting runtime error?
Link to my code is https://www.codechef.com/viewsolution/9950306

i tried to sorted the input array in descending order and then subtract elements in 1 to n from 0: input*-input[0]…this gives me the n required elements…please tell me what is wrong with this approach