Black_hat123's blog

By Black_hat123, history, 19 months ago, In English

Hello, can any one please tell how to solve This problem

 
 
 
 
  • Vote: I like it
  • +18
  • Vote: I do not like it

»
19 months ago, # |
Rev. 4   Vote: I like it +8 Vote: I do not like it

Well, if n was <= 20 we could've simply ran through every subset of the the array, but n <= 40. Observe that 40 is still pretty close to 20.

So what should we do?

Solution

We are going to use the Meet in the Middle algorithm (also the name of the task :)) to solve this problem. That means that we generate all sums for all subsets of the first half and the second half of the array separately. That way time complexity is O (2^(n/2)*n)

Now that we have the sums for both halves we want to find in how many ways we can pair two from different halves so that we get x.

We can do that easily by sorting the sum arrays of both halves and use the two pointers method.

  • »
    »
    6 weeks ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    and When n=80 or n=60 then what to do ?? i.e n=80 , i devide array into 4 sub_array, a1 a2 a3 a4 . But How can i proceed further ?? [user:apostoldaniel854]can you please explain it to me??