game_changer007's blog

By game_changer007, 9 years ago, In English

Hi, I tried to solve this question,but could not. I looked at the solutions but didnt get much.

How to solve this problem efficiently!!

»
9 years ago, # |
  Vote: I like it +3 Vote: I do not like it

For a given len length interval, suppose there are l1 positive numbers and l2 negative numbers. Now, the maximum modular sum can be obtained from the maximum of 2 values — you change as many as possible negative numbers to positive or you change as many as possible positive numbers to negative. If you cannot change all then change the k maximum by magnitude numbers. The maximum of these 2 quantities is the maximum modular sum of that interval. So for a given interval if you know the k smallest negative numbers and the k largest positive numbers and the total sum of the interval then you can find out the answer for that interval. To do this for all intervals, maintain multisets of the required values and keep on adding new and removing the oldest elements as you go, just like you do in a sliding window problem. I hope now you can understand the implementations.