There are two arrays A and B whose sizes are N. By exchanging elements in A and B, sum (a)-sum (B) is minimized.

Source: Internet
Author: User
Tags integer numbers

There are two arrays A and B, all of which are N in size. The values of the array elements are unordered due to arbitrary integer numbers;
Requirement: by exchanging elements in A and B, the difference between the sum of Elements A and B is minimized.


The sum of the current array A and array B is
A = sum (a)-sum (B)

After the I-th element of A is exchanged with the J-th element of B, the sum of A and B is
A' = sum (a)-A [I] + B [J]-(sum (B)-B [J] + A [I])
= Sum (a)-sum (B)-2 (A [I]-B [J])
= A-2 (A [I]-B [J])
Set x = A [I]-B [J]

| A |-| a' | = | A |-| A-2x |

Suppose a> 0,

When X is between (0, A), this exchange can reduce the sum of A and B after the exchange. The closer X is to A/2, the better the effect,

If no X is found between (0, A), the current A and B are the answer.

The algorithm is roughly as follows:

Find the I and j that make X Between (0, A) and closest to A/2 in A and B, exchange the corresponding I and j elements, and recalculate, repeat the preceding steps until the X Between (0, a) is not found.

Int sum1, sum2, A; // respectively represents the sum of a [] and B [], and the difference of 2: int temp; bool dayu0; // whether the difference between the sum and is greater than 0 int pos1 and pos2; // subscripts of a [I] and B [J] awaiting exchange I j float Minn; // A [I]-B [J] value bool have1 closest to A/2; // whether the while (1) {sum1 = 0; sum2 = 0; for (INT I = 0; I <n; ++ I) // calculate the sum of the two arrays {sum1 + = AR1 [I]; sum2 + = AR2 [I];} A = sum1-sum2; // The difference between dayu0 = a> 0? True: false; // whether the difference is greater than 0 or less than 0 have1 = false; // whether the solution can be found for (INT I = 0; I <n; ++ I) // find the [I]-B [J] {for (Int J = 0; j <n; ++ J) closest to A/2) {temp = AR1 [I]-AR2 [J]; If (dayu0 & temp> 0 & temp <A) | (! Dayu0 & temp <0 & temp> A) // If a [I]-B [J] is between (0,) {If (have1 & ABS (temp-A/2.0) <Minn) // if it is closer to a/2 than a [I]-B [J], update {Minn = ABS (temp-A/2.0); pos1 = I; pos2 = J ;}else {have1 = true; Minn = ABS (temp-A/2.0); pos1 = I; pos2 = J ;}}} if (! Have1) // if no matching a [I]-B [J] is found, {break;} swap (AR1 [pos1], AR2 [pos2]) ends. // exchange elements in a B}

  

There are two arrays A and B whose sizes are N. By exchanging elements in A and B, sum (a)-sum (B) is minimized.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.