You are given an array A of N integers (N is even). Split array A into N/2 pairs in such a way that the largest sum of any pair will be as small as possible. Each element has to belong to exactly one pair.

For example, given array A = [4, 5, 3, 1], we can split it into pairs in three ways:

(4, 5) and (1, 3), where the sums of the pairs' elements are 9 and 4. The largest sum is 9.

(4, 1) and (5, 3), where the sums of the pairs' elements are 5 and 8. The largest sum is 8.

(4, 3) and (5, 1), where the sums of the pairs' elements are 7 and 6. The largest sum is 7.

In the above splits, pairs with the largest sums of elements are (4, 5), (5, 3) and (4, 3). Of these, the smallest is 4 + 3 = 7.

Write a function:

class Solution { public int solution(int[] A); }

that, given an array A of N integers, returns the largest sum of the pairs' elements in the optimal split.

Examples:

1. Given A = [4, 5, 3, 1], the function should return 7, as explained above.

2. Given A = [2, 3, 1, 1], the function should return 4. The optimal split is (1, 3) and (1, 2), where the sums of pairs are 1 + 3 = 4 and 1 + 2 = 3, and 4 is the larger.

3. Given A = [−5, −6], the function should return −11. We can make only one pair, (−5, −6), whose sum is −11.

4. Given A = [2, 2, 2, 2, 2, 2], the function should return 4. The only possible split is (2, 2), (2, 2) and (2, 2), where the sums of all pairs are equal to 4.

Write an efficient algorithm for the following assumptions:

N is an even integer within the range [2..100,000];

each element of array A is an integer within the range [−1,000,000,000..1,000,000,000].

#include
#include

using namespace std;

int solution(vector A){
cerr << "Tip: Use cerr to write debug messages on the output tab.";
return 0;

Respuesta :

You are given an array A of N integers (N is even). In order to make the largest sum of any pair as small as possible, divide array A into N/2 pairs.

}

int main()

{

   vector A = {4, 5, 3, 1};

   cout << solution(A);

   return 0;

}

What is array?
An array is a type of data structure used in computer science that contains a set of elements (values and variables), each of which is identified by an array index or key. An array is stored in a way that allows a mathematical formula to determine each element's position given its index tuple. A linear array, sometimes referred to as a one-dimensional array, is the most basic sort of data structure. For instance, ten words with memory addresses 2000, 2004, 2008,..., 2036 (or 0x7D0, 0x7D4, 0x7D8,..., 0x7F4) may be used to store an array of ten 32-bit (4-byte) integer variables with indices 0 through 9. This would give the element with index I the address 2000 + I 4) in this case. The first address, foundation address, and base address is the memory location of the first element of an array.

To learn more about array
https://brainly.com/question/28632808
#SPJ4