For a given array $$$a_i$$$ , we need to find all subsets withing given range [A,B].

For an array consisting of $$$a_1$$$ and $$$a_2$$$ all subsets are: {},{$$$a_1$$$},{$$$a_2$$$},{$$$a_1$$$,$$$a_2$$$}.

Their sums are 0, $$$a_1$$$, $$$a_2$$$, $$$a_1$$$+$$$a_2$$$.

Input:

```
3 -1 2
1 -2 3
```

Output:

```
5
```

The 5 subsets that fit between -1 and 2 are : {} , {$$$a_1$$$},{$$$a_1$$$,$$$a_2$$$},{$$$a_1$$$,$$$a_2$$$,$$$a_3$$$},{$$$a_2$$$,$$$a_3$$$}.

I did the math and I think I got the task, I got how to solve it, I just don't know how to implement it.

My way is calculating the sum of all elements, and then reducing each element at a time to produce more subsets, and from those subsets to produce even more (unique of course) subsets, all while checking if each subset is within range of given [A,B], but I don't know how to implement it, I've never done this type of a task before. My way would have time complexity of O(2^n) and constraints on this program are 1<n<=34, -2e7 <= $$$a_i$$$ <= 2e7, -5e8 <= $$$a_i$$$ <= 5e8, would this even pass theoretically ?

The statement you've given is a bit confusing. I am presuming you mean you want all subsets

with sumin the range [A, B].Your idea seems to be to use bruteforce to generate all subsets, which are $$$2^n$$$ as you've correctly found. This can be implemented quite concisely using recursion. Feel free to ask if you can't seem to figure out a nice way to implement it.

The problem, however, is not intended to be solved like this. Usually $$$O(2^n)$$$ solutions have the constraint $$$n<=20$$$ or at most $$$n<=25$$$, but for $$$n=34$$$ the solution is most likely not that.

The real solution, however, is not

thatdifferent. It's a technique called "meet in the middle". You can look it up or you can ask for more details about it, but if you struggle implementing the regular bruteforce, perhaps you should focus more on the basics.I've actually written down in detail this task and how I would solve it but some bug happened and I lost all my text so I just made this mini version instead, I should have mentioned that the sums are what matters, sorry. I will google this and try to implement it, and if I don't understand it still, I'll ask here, thank you.

Can you explain more about how to solve it use this using "meet in the middle"? Also, can you explain "meet in the middle" or any useful links and questions?

Here's the solution: https://pastebin.ubuntu.com/p/ZpBQP6SMmr/

Its not my solution and I'm trying to figure it out, but you can google Meet in the Middle algorithm and learn it. https://cses.fi/book/book.pdf

From page 54, you can read a bit on this algorithm.

$$$O(NB/64)$$$ possible with bitset. Should pass if the TL is 3 seconds:

^ this assumes $$$A, B$$$ both are positive. If you want to handle negative numbers too, then you need to offset the indexes. That is number $$$i$$$ goes to position $$$i + offset$$$, and becomes positive.

What about the case: 4 5 5 1 2 3 4 ?

A and B can definitely be negative, they can be -5e8 negative in fact.