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 755709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:09:41+00:00 2026-05-14T15:09:41+00:00

Greetings, Any input on a way to divide a std::vector into two equal parts

  • 0

Greetings,

Any input on a way to divide a std::vector into two equal parts ? I need to find the smallest possible difference between |part1 – part2|.

This is how I’m doing it now, but from what you can probably tell it will yield a non-optimal split in some cases.

auto mid = std::find_if(prim, ultim, [&](double temp) -> bool
{
    if(tempsum >= sum)
        return true;

    tempsum += temp;
    sum -= temp;
    return false;
});

The vector is sorted, highest to lowest, values can indeed appear twice.
I’m not expecting part1 and part2 to have the same numbers of elements, but sum(part1) should be as close as possible to sum(part2)

For example if we would have { 2.4, 0.12, 1.26, 0.51, 0.70 }, the best split would be { 2.4, 0.12 } and { 1.26, 0.51, 0.70 }.

If it helps, I’m trying to achieve the splitting algorithm for the Shannon Fano encoding.

Maybe this will help you guys understand my question better http://en.wikipedia.org/wiki/Shannon%E2%80%93Fano_coding#Example

Any input is appreciated, thanks!

  • 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-05-14T15:09:41+00:00Added an answer on May 14, 2026 at 3:09 pm

    Given that:

    The vector is sorted, highest to lowest, values can indeed appear twice.
    I'm not expecting part1 and part2 to have the same numbers of elements, but
    sum(part1) should be as close as possible to sum(part2)

    This is not optimal, but it will provide a reasonable approximation for values such as those you have given (unless I mucked something up … I didn’t actually compile or test it). It also works if you have negative numbers in the original vector:

    std::pair<std::vector<double>, std::vector<double> >
        split(const std::vector<double>& data)
    {
        std::pair<std::vector<double>, std::vector<double> > rv;
        double s1=0.0, s2=0.0;
        std::vector<double>::const_iterator i;
    
        for (i=data.begin(); i != data.end(); ++i)
        {
            double dif1 = abs(*i + s1 - s2);
            double dif2 = abs(*i + s2 - s1);
    
            if (dif1 < dif2)
            {
                rv.first.push_back(*i);
                s1 += *i;
            }
            else
            {
                rv.second.push_back(*i);
                s2 += *i;
            }
        }
        return rv;
    }
    

    EDIT: The quality of the result will be lower with this approach if there the sum of the negative numbers in your vector overwhelms the sum of the positive numbers in the list. To resolve, you could try to order the original list by descending absolute value rather than in strictly descending order.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.