Given an array of 'n' integers. We are allowed to apply sign change operation on all elements. How can we find the minimum possible non-negative array sum efficiently?
-
For example: Consider this array A= { 2, 1, 3, 4, 2}. Since A can be changed to A' = { -2, -1, -3, 4, 2 },the minimum possible non-negative sum is zero for this array.
-
Answer:
Same as problem of splitting the array into 2 subarrays such that the difference between the sum of 2 sub-arrays in minimum.
Anonymous at Quora Visit the source
Other answers
This is just the http://en.wikipedia.org/wiki/Partition_problem / http://en.wikipedia.org/wiki/Subset_sum_problem in disguise. You have to partition the given numbers (or, more precisely, their absolute values) into two piles that have sums as close to each other as possible. (In our case, the two piles are "numbers that will have the + sign" and "- sign".)
Michal Forišek
You can think about this problem as two stacks and you try to minimize the difference of their height. This is the offline scheduling problem and it is solvable by a greedy algorithm. Sort the list (max to min) for each object, lay it on the stack with the lowest height code snippet:- http://stackoverflow.com/a/25255629 source:- http://stackoverflow.com/questions/14272380/given-one-can-change-the-sign-of-numbers-find-minimum-sum-of-array
Akib Khan
Related Q & A:
- How can I find the minimum number of open rectangles in a grid?Best solution by Stack Overflow
- How can I find and replace html links with Notepad++?Best solution by stackoverflow.com
- How can I find out the time of sunrise?Best solution by Yahoo! Answers
- How can I find out how many points i have on my license?Best solution by Yahoo! Answers
- How can I find an online job that I can start for free?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.