Find the maximum result after collapsing an array with subtractions
The other answers are fine, but here's another way to think about it:
If you expand the result into individual terms, you want all the positive numbers to end up as additive terms, and all the negative numbers to end up as subtractive terms.
If you have both signs available, then this is easy:

Subtract all but one of the positive numbers from a negative number
Subtract all of the negative numbers from the remaining positive number

If all your numbers have the same sign, then pick the one with the smallest absolute value at treat it as having the opposite sign in the above procedure.  That works out to:

If you have only negative numbers, then subtract them all from the least negative one; or
If you have only positive numbers, then subtract all but one from the smallest, and then subtract the result from the remaining one.
Given an array of integers, I need to reduce it to a single number by repeatedly replacing any two numbers with their difference, to produce the maximum possible result.
Example1 - If I have array of [0,-1,-1,-1] then performing (0-(-1)) then (1-(-1)) and then (2-(-1)) will give 3 as maximum possible output
Example2- [3,2,1,1] we can get maximum output as 5 { first (1-1) then (0-2) then (3-(-2)}
Can someone tell me how to solve this question?