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

  • SEARCH
  • Home
  • 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 6638569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:27:05+00:00 2026-05-25T23:27:05+00:00

I am hunting job now and doing many algorithm exercises. Here is my problem:

  • 0

I am hunting job now and doing many algorithm exercises. Here is my problem:


Given two arrays: a and b with same length, the subject is to make |sum(a)-sum(b)| minimal, by swapping elements between a and b.


Here is my though:

assume we swap a[i] and b[j], set Delt = sum(a) – sum(b), x = a[i]-b[j]
then Delt2 = sum(a)-a[i]+b[j] – (sum(b)-b[j]+a[i]) = Delt – 2*x,
then the change = |Delt| – |Delt2|, which is proportional to |Delt|^2 – |Delt2|^2 = 4*x*(Delt-x),

Based on the thought above I got the following code:


Delt = sum(a) - sum(b);
done = false;
while(!done)
{
    done = true;
    for i = [0, n)
    { 
        for j = [0,n)
        {
            x = a[i]-b[j];
            change = x*(Delt-x);
            if(change >0)
            {
                 swap(a[i], b[j]);
                 Delt = Delt - 2*x;
                 done = false;
            }
        }
    }
}

However, does anybody have a much better solution ? If you got, please tell me and I would be very grateful of you!

  • 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-25T23:27:06+00:00Added an answer on May 25, 2026 at 11:27 pm

    This problem is basically the optimization problem for Partition Problem with an extra constraint of equal parts. I’ll prove that adding this constraint doesn’t make the problem easier.

    NP-Hardness proof:

    Assume there was an algorithm A that solves this problem in polynomial time, we can solve the Partition-Problem in polynomial time.

    Partition(S):
    for i in range(|S|):
       S += {0}
       result <- A(S\2,S\2) //arbitrary split S into 2 parts
       if result is a partition: //simple to check, since partition is NP.
         return true.
    return false //no partition
    

    Correctness:

    If there is a partition denote as (S1,S2) [assume S2 has more elements], on iteration |S2|-|S1| [i.e. when adding |S2|-|S1| zeros]. The input to A will contatin enough zeros so we can return two equal length arrays: S2,S1+{0,0,…,0}, which will be a partition to S, and the algorithm will yield true.

    If the algorithm yields true, and iteration k, we had two arrays: S2,S1, with same number of elements, and equal values. by removing k zeros from the arrays, we get a partition to the original S, so S had a partition.

    Polynomial:

    assume A takes P(n) time, the algorithm we produced will take n*P(n) time, which is also polynomial.

    Conclusion:

    If this problem is solveable in polynomial time, so does the Partion-Problem, and thus P=NP. based on this: this problem is NP-Hard.

    Because this problem is NP-Hard, for an exact solution you will probably need an exponential algorith. One of those is simple backtracking [I leave it as an exercise to the reader to implement a backtracking solution]

    EDIT: as mentioned by @jpalecek: by simply creating a reduction: S->S+(0,0,...,0) [k times 0], one can directly prove NP-Hardness by reduction. polynomial is trivial and correctness is very similar to the above partion’s correctness proof: [if there is a partition, adding ‘balancing’ zeros is possible; the other direction is simply trimming those zeros]

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

Sidebar

Related Questions

Lately I've been job hunting, and for the most part, they would ask me
Here's a question that's been haunting me for a year now. The root question
Hunting internationalization bugs here. Does mysql has a variable which can be set per
I've been hunting bugs for the past three days and I've kind of given
I have a problem i have been hunting down for the last day and
The last two days I spent with hunting down memory leaks. I´ve read the
I've been hunting around the net now for a few days trying to figure
I've spent the last two days hunting JavaScript memory leaks in my web project
Quick background: I've been hunting down a Maven / Surefire test-running problem for days
I just need clarification about something. I am currently job hunting - I put

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.