How to change array elements?

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

Was this solution helpful to you?

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:

Just Added Q & A:

Find solution

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.