Categories

# What is the minimum number of swaps needed so that the difference of sums of arrays a and b is minimum?

Given 2 arrays of integers `a[]` and `b[]` with the same size of `n` numbered from `1 to n`.

You can swap any `a[i]` with `b[i]`.

What is the minimum number of swaps needed so that the difference of the sums of array `a[]` and `b[]` is minimum ?

Then print out:

• The number of swaps
• The swapped indexes
• The difference of sums of both arrays

Example

``````n = 6

a[] = { 1, 1, 4, 4, 0, 6 }

b[] = { 6, 3, 1, 1, 6, 1 }
``````

Result

`````` - 2 (The number of swaps)
- 5, 6 (The swapped indexes)
- 0 (The difference of sums of the arrays)
``````

Explanation

If you swap `a[5]` with `b[5]` and `a[6]` with `b[6]` which requires 2 swaps, arrays `a[]` and `b[]` will become:

``````a[] = {1, 1, 4, 4, 6, 1}

b[] = {6, 3, 1, 1, 0, 6}
``````

Sum of `a[]` is `1 + 1 + 4 + 4 + 6 + 1 = 17`

Sum of `b[]` is `6 + 3 + 1 + 1 + 0 + 6 = 17`

So the difference of the two sums is 0.

PS: I still need a full explanation or a `C` or `C++` code for this problem