I have an array of integers (both positive and negative). I need to make all the elements of the array zero, by doing the following operation repeatedly. Increasing one element by 1 and decreasing the others by 1 . I need to do this in minimum number of steps (if it is possible to do so). My approach is to sort the integers and then increase the smallest element and decrease all others and keep on doing this until all become zero. But I am not sure, can anyone tell me if there is a seperate correct approach ?
for e.g. if the array is {1,1,3} then according to my approach {2,0,2} -> {1,1,1} -> {2,0,0} -> {1,-1,1} -> {0,0,0}