Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8330791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T02:13:19+00:00 2026-06-09T02:13:19+00:00

I have an total amount payable by an Employer this amount needs to be

  • 0

I have an total amount payable by an Employer this amount needs to be split amongst 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 I must call a 3rd party system to assign these amounts one by one. But I cannot let the balance go below $0 or above the total amount ($400) during the allocation.

So if I insert in the above order a,b,c will work so the current allocated sum = 100 + 200 – 200 = $100.
However when I try to allocate d. The system will try to add -$200 which will make the current allocated sum -$100 which is < $0 which is not allowed so it will be rejected by the system.

If I sort the list so negative items are last. i.e.

a $100
b $200
e $500
c -$200
d -$200

a will work, b will work, but when it tries to insert e there will be insufficient funds error because we have exceeded the maximum of $400. I have come to the realisation that there is no silver bullet and there will always be scenarios what will break. However I wanted to come up with a solution that would work most of the time.

Normal sample of data would have between 5 – 100 items. With only 2-15% of those containing negative amounts.

Is there a clever way I can sort the list? Or would it be better just to try an allocated multiple times. For example split the positive and negatives into two list. Insert positives until one errors, then insert negatives until it errors then switch back and forth between the list until it is all allocated or until both of them error.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T02:13:20+00:00Added an answer on June 9, 2026 at 2:13 am

    Although this effectively the same as Haile’s answer (I started working on an answer before he posted his, and beat me to the punch) I thought I would post it anyway since it includes some source code and may help someone who wants a concrete implementation (sorry that it’s not in C#, C++ is the closest thing I have access to at the moment)

    #include <iostream>
    #include <vector>
    #include <numeric>
    #include <algorithm>
    
    using namespace std;
    
    vector<int> orderTransactions(const vector<int>& input) {
    
        int max = accumulate(input.begin(), input.end(), 0);
    
        vector<int> results;
        // if the sum is negative or zero there are no transactions that can be added
        if (max <= 0) {
            return results;
        }
    
        // split the input into positives and negatives
        vector<int> sorted = vector<int>(input);
        sort(sorted.begin(), sorted.end());
    
        vector<int> positives;
        vector<int> negatives;
    
        for (int i = 0; i < sorted.size(); i++) {
            if (sorted[i] >= 0) {
                positives.push_back(sorted[i]);
            } else {
                negatives.push_back(sorted[i]);
            }
        }   
    
        // try to process all the transactions
        int sum = 0;
        while (!positives.empty() || !negatives.empty()) {
            // find the largest positive transaction that can be added without exceeding the max
            bool positiveFound = false;
    
            for (int i = (int)positives.size()-1; i >= 0; i--) {
                int n = positives[i];
                if ((sum + n) <= max) {
                    sum += n;
                    results.push_back(n);
                    positives.erase(positives.begin()+i);
                    positiveFound = true;
                    break;
                }
            }
    
            if (positiveFound == true) {
                continue;
            }
    
            // if there is no positive find the smallest negative transaction that keep the sum above 0
            bool negativeFound = false;
            for (int i = (int)negatives.size()-1; i >= 0; i--) {
                int n = negatives[i];
                if ((sum + n) >= 0) {
                    sum += n;
                    results.push_back(n);
                    negatives.erase(negatives.begin()+i);
                    negativeFound = true;
                    break;
                }
            }
    
            // if there is neither then this as far as we can go without splitting the transactions
            if (!negativeFound) {
                return results;
            }
        }
    
        return results;
    }
    
    
    int main(int argc, const char * argv[]) {
    
        vector<int> quantities;
        quantities.push_back(-304);
        quantities.push_back(-154);
        quantities.push_back(-491);
        quantities.push_back(-132);
        quantities.push_back(276);
        quantities.push_back(-393);
        quantities.push_back(136);
        quantities.push_back(172);
        quantities.push_back(589);
        quantities.push_back(-131);
        quantities.push_back(-331);
        quantities.push_back(-142);
        quantities.push_back(321);
        quantities.push_back(705);
        quantities.push_back(210);
        quantities.push_back(731);
        quantities.push_back(92);
        quantities.push_back(-90);
    
        vector<int> results = orderTransactions(quantities);
    
        if (results.size() != quantities.size()) {
            cout << "ERROR: Couldn't find a complete ordering for the transactions. This is as far as we got:" << endl;
        }
    
        for (int i = 0; i < results.size(); i++) {
            cout << results[i] << endl;
        }
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have data of this form: Category Source Amount Dues FTW $100 Donations ODP
i have payment table fields update reason and amount & total field are change
I have a problem with sql (maths). I have a total payable given to
I have a datagridview column 'total',in which I am doing sum of rates of
I have Total Amount Stored in Some Variable,Two TextBoxes, Discount Percentage Discount Amount that
I have an invoice table. When I change a total amount to be paid,
I have got a total product page which shows the total amount of the
Trying to find: Show the top-20 PIs who have the largest total amount of
I have a javascript function to calculate total amount : <script> function calculate(v) {
I have a WizardStep Control from which i need one information: The total amount

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.