A system I'm building includes a set of UI sliders (the number varies) each with a scale of 0-100. By slider I mean a UI where you grab an element and drag it up and down, like a volume control. They are connected by an algorithm that ensures they always total 100. So when one slider is moved up, the others all move down, eventually to zero. When one is moved down, the others move up. At all times the total must be 100. So here sliders have various values, but they total 100%:
----O------ 40 O---------- 0 --O-------- 20 --O-------- 20 --O-------- 20
If the first slider then moves UP from 40 to 70, the others have to move DOWN in value (as the slider is dragged). Note three sliders changed from 20 to 10, and one stayed at zero as it cannot go lower.
-------O--- 70 O---------- 0 -O--------- 10 -O--------- 10 -O--------- 10
Of course when any slider reaches 0 or 100, it cannot move any further, which is where my head really starts to hurt. So if a slider moves higher, the others move lower, but when any of them reach zero only the remaining ones that have not yet reached zero can move lower.
I'm asking this here as this question is specific to the algorithm not the implementation. FWIW the platform is Android Java, but that's not especially relevant.
The approach I took with my first stab was to calculate the percentage change of the slider that moved. I then split that change and applied it (in the other direction) to the other slider values. The problem though is that by using percentages and multiplying, if any slider gets to zero, it can never be increased again from zero - the net result of that is that individual sliders get stuck at zero. I've used sliders with a range of 0 - 1,000,000 to avoid rounding issues and that appears to be helpful, but I've yet to create an algorithm that handles all scenarios well.