I have the total amount paid by the Employer, this amount should be divided between the staff.
for example
a $100 b $200 c -$200 d -$200 e $500
The total amount payable is the sum of all items, in this case $ 400
The problem is that I have to call a third-party system to assign these amounts one at a time. But I cannot allow the balance to remain below $ 0 or above the total amount ($ 400) during the distribution.
So, if I insert into the above order, then a, b, c will work so that the current allocated amount = 100 + 200 - 200 = 100 dollars. However, when I try to highlight d. The system will try to add - $ 200, which will make the current allocated amount - $ 100, $ 0, which is not allowed, so the system will be rejected.
If I sort the list, the negative elements will be the last. i.e.
a $100 b $200 e $500 c -$200 d -$200
a will work, b will work, but when he tries to insert e, there will be a lack of funds, because we have exceeded a maximum of $ 400. I came to understand that there is no silver bullet, and there will always be scenarios of what will break. However, I wanted to find a solution that would work most of the time.
A normal data sample will contain from 5 to 100 units. Only 2-15% of them contain negative amounts.
Is there a trick to sorting the list? Or it would be better to just try highlighted several times. For example, divide the positive and negative into two lists. Insert positive values until one error, then insert negatives until errors appear, then switch back and forth between the list until it is highlighted, or until both errors are.