You are given an array containing 2 \times n elements.You have to partition the numbers in to n pairs with the property that partition minimize the maximum sum of a pair.
Input
First line contains T number of test cases. Next line contain N number of pairs.
Next line contain 2*N positive integers represent elements of an array.
Output
For each test case print minimum possible maximum sum of a pair.
CONSTRAINTS
1 ≤ T ≤ 100
1 ≤ N ≤ 100000
1 ≤ A[i] ≤ 100000
SAMPLE INPUT
1
3
5 6 2 3 9 8
SAMPLE OUTPUT
11
Explanation
In sample input possible partitions are ((5,6) , (9,2) , (8,3)) and pair sum for these partitons are (11,11,11) so minimum possible maximum sum is 11.
Other possible pairs are ((3,2) , (9,5) , (8,6)) so here minimum possible maximum sum is 14. So 11 is minimum possible maximum sum from all possible pair of partitions.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We are asked: given 2n numbers, partition them into n pairs. For each partition, we can compute the sum of each pair; the "maximum sum" of a pair for that partition is the maximum over pairs of the sum of the two numbers. We need to find the minimum possible such maximum sum over all possible pairings. Output that value.
This is a classic optimization problem: pair up numbers to minimize the maximum pair sum. The typical solution is to sort the array, then pair the smallest with the largest, second smallest with second largest, etc., and then compute the maximum sum of these pairs. That yields the minimal possible maximum sum. But is that always optimal? Let's think.
We have an array of numbers. We want to partition into pairs to minimize the maximum sum. Intuitively, to balance loads, we want to avoid pairing two large numbers together, as that would increase the max sum. Pairing large with small reduces the maximum. The sorted pairing of min with max is a known strategy for minimizing the maximum sum (or for "fair" division). I recall problems like "Given 2N numbers, pair them to minimize the maximum sum" and the optimal solution is indeed sorting and pairing min with max. This can be proven via exchange argument: suppose we have an optimal pairing where the largest number is not paired with the smallest. Then we can improve by swapping partners.
Alternatively, think of it as: after sorting a[0] <= a[1] <= ... <= a[2N-1], we need to pair them. The minimal possible maximum sum S is at least max(a[0]+a[2N-1], a[1]+a[2N-2], ...). Actually, the pairing (a[i] with a[2N-1-i]) yields sums s_i = a[i] + a[2N-1-i]. The maximum among these sums is a candidate. To prove it's minimal: for any pairing, consider the sorted order; the maximum sum will be at least as large as the maximum of those sums due to a combinatorial inequality (Dilworth?).
Actually, there's a known result: To minimize the maximum sum of pairs, sort and pair smallest with largest. Similarly, to minimize the sum of maximums of pairs, or something else. But we need to verify.
We could also think about binary search on answer S: check if it's possible to pair numbers so that each pair sum <= S. That reduces to a matching problem: given multiset of numbers, can we pair them such that sum <= S? For a given S, we can greedily check: sort, then pair smallest with largest possible such that sum <= S. This is similar to the "two-pointer" method: after sorting, check if for all i, a[i] + a[2N-1-i] <= S. But is that condition sufficient? If a[i]+a[2N-1-i] <= S for all i, then the pairing works. Conversely, if there exists a pairing with all sums <= S, then after sorting, it must hold that a[i] + a[2N-1-i] <= S? Not necessarily; but I think it's necessary that the largest element cannot be paired with an element larger than S - a[smallest]? Hmm, but it's known that if the greedy pairing (smallest with largest) works for S (i.e., all those sums <= S), then S is feasible. And if S is feasible, then the sorted pairing must also satisfy the condition? Actually, consider numbers [1,2,3,100]. S=103 is feasible: we could pair (1,2)=3 and (3,100)=103 -> max 103, but the sorted pairing would be (1,100)=101, (2,3)=5, max=101 which is <=103, so condition holds. Suppose numbers [1,50,60,100]. S=110 is feasible? Try pair (1,60)=61, (50,100)=150 >110; (1,100)=101, (50,60)=110 => max=110 feasible. Sorted pairing: (1,100)=101, (50,60)=110 -> max=110 => condition holds. For S=101? Check feasibility: can we pair all sums <=101? (1,100)=101 ok, (50,60)=110 >101 not ok; alternative (1,60)=61, (50,100)=150 >101 not ok; so not feasible. Sorted pairing gives max=110 >101, fails. So condition seems both necessary and sufficient? Let's test a counterexample: [1,4,5,6]. n=2. Sorted pairing: (1,6)=7, (4,5)=9 => max=9. Could we achieve max=8? Check if feasible: need each pair sum <=8. Options: (1,6)=7 ok, (4,5)=9 >8, not ok; (1,5)=6, (4,6)=10 >8; (1,4)=5, (5,6)=11 >8. So not feasible. S=9 minimal. Sorted pairing yields max=9. So it's optimal.
But consider [1,2,3,4,5,6] n=3. Sorted pairing: (1,6)=7, (2,5)=7, (3,4)=7 => max=7. Is that optimal? Could we get max=6? Pair sums all <=6? Minimal possible max is at least ceil((sum of all)/n) = (21)/3=7, so 7 is lower bound. So 7 is optimal. Good.
Consider [1,1,100,100]. n=2. Sorted pairing: (1,100)=101, (1,100)=101 => max=101. Could we get max=101? Obviously. Could we get max=100? Possibly pair (1,1)=2, (100,100)=200 >100, no. So 101 is minimal.
Consider [1,2,3,10]. n=2. Sorted pairing: (1,10)=11, (2,3)=5 => max=11. Is there a pairing with max=10? Try (1,3)=4, (2,10)=12 >10; (1,2)=3, (3,10)=13 >10. So not feasible. So 11 minimal.
Thus the sorted pairing is optimal.
Proof sketch: For any valid pairing, after sorting numbers, the maximum sum is at least max_i (a[i] + a[2n-1-i]). Because consider the k smallest and k largest elements... Actually, we can use the rearrangement inequality: For any pairing, the maximum sum is minimized when we pair smallest with largest.
Thus solution: For each test case, read n, then array of size 2n. Sort array. Then compute sum for i from 0 to n-1: a[i] + a[2n-1-i]. Find the maximum among these n sums. Output that.
Constraints: T up to 100, N up to 100,000, so total elements across test cases could be up to 100 * 200,000 = 20 million, which is large but manageable with O(total elements log elements) if we sort each test case separately. Sorting each 2n size array O(2n log(2n)). With N up to 100k per test case, that's fine within time limits presumably.
But note: The sum of N over test cases might be up to 100k? Actually "1 ≤ N ≤ 100000" per test case; T up to 100; but there is no explicit sum of N bound. However, total number of elements across test cases could be up to 100 * 200,000 = 20 million, which is okay for sorting each individually with a fast sorting algorithm. But 20 million integers, sorting each test case: worst-case 100 test cases each with 200k elements = 20 million total elements; sorting each 200k sized array is O(200k log 200k) per test case, which times 100 ~ 100 * 200k * log2(200k) ~ 100 * 200k * 18 = 360 million operations, might be borderline but likely acceptable in C++/Java with good implementation. But the problem might be from an online judge expecting O(n log n) per test case; maybe they expect T small or sum of 2n across test cases <= something? However, the problem statement didn't specify that. But we can implement efficiently. In Python, sorting 20 million integers might be heavy but possibly okay if T is small; but typical constraints for Python might be lower. But we should aim for efficient I/O.
Given that the problem statement is shown as sample, maybe T is small. However, to be safe, we can perhaps do the following: Use fast input reading (sys.stdin.buffer.read). Then parse all numbers, process test cases.
But the algorithm itself is straightforward.
Edge cases: N=1, array size 2, then the only pair sum is the answer, which is sum of the two numbers. Sorted pairing yields that.
Check for correctness: For each test case, we compute the maximum sum after pairing min-max. Ensure that we handle large values: A[i] up to 100,000, so sums up to 200,000, fits in int.
Thus solution: Read T, then for each test case, read N, then read 2N integers. Sort. Compute max_sum = max(arr[i] + arr[2N-1-i] for i in range(N)). Print max_sum.
Now, provide explanation.
But the user asks: "You are given an array containing 2 \times n elements.You have to partition the numbers in to n pairs with the property that partition minimize the maximum sum of a pair." So indeed it's the classic problem.
Thus answer: we need to produce code that does that.
But the prompt says: "You are given an array containing 2 \times n elements... For each test case print minimum possible maximum sum of a pair." So we will produce a solution.
But the instruction might be to output for each test case.
We'll also include a brief explanation and complexity analysis.
Now, regarding language: The user hasn't specified a language; likely they expect an answer in a programming language like C++ or Python. I can provide both? But the typical answer on such platforms: Since the problem may be from code challenges, I can provide a solution in C++ and Python. I'll choose Python for readability, but ensure it's efficient.
Let's design Python code:
import sys
def main():
data = list(map(int, sys.stdin.buffer.read().split()))
it = iter(data)
t = next(it)
out = []
for _ in range(t):
n = next(it)
size = 2 * n
arr = [next(it) for _ in range(size)]
arr.sort()
max_sum = 0
for i in range(n):
s = arr[i] + arr[size - 1 - i]
if s > max_sum:
max_sum = s
out.append(str(max_sum))
sys.stdout.write("\n".join(out))
if name == "main":
main()
That should handle up to 20 million numbers. But list comprehension for each test case might be okay. However, using [next(it) for _ in range(size)] creates a list each time; that's fine.
Potential improvements: Use array slicing to compute sums quickly: we could compute max of sums using a loop; but loop in Python over up to 100k per test case is fine.
Thus final answer: Provide the solution.
But the user might be expecting the reasoning and code. As ChatGPT, we should output both explanation and code.
Given the instruction: "You are given an array containing 2 \times n elements.You have to partition the numbers in to n pairs with the property that partition minimize the maximum sum of a pair. Input ... Output ..." So likely they want a solution. I'll produce a final answer containing description of algorithm and code.
But I'll also include note about proof.
Now final output. ### Approach
The problem asks to partition (2N) numbers into (N) pairs such that the maximum sum among all pairs is minimized.
This is a classic minimax pairing problem, and the optimal strategy is to sort the array and pair the smallest element with the largest, the second smallest with the second largest, and so on. The answer is the maximum of the sums of these pairs.
Why this works?
By sorting, we know the smallest element must be paired with some other element. If we pair it with any element other than the largest, the largest element would have to be paired with some other element, which would create a sum at least as large as the sum with the smallest. A standard exchange argument shows that any deviation from the sorted min–max pairing cannot improve the maximum sum.
Feasibility Check (Alternative View):
We can also binary search the answer (S) and test if a pairing with all sums (\le S) exists. The test reduces to checking whether after sorting, (a[i] + a[2N-1-i] \le S) for all (i). This condition is both necessary and sufficient, so the smallest such (S) is exactly the maximum of those pairwise sums.
Complexity
Time: (O((2N) \log (2N))) per test case due to sorting.
Space: (O(2N)) for storing the array.
Implementation (Python)
python
import sys
def solve() -> None:
data = list(map(int, sys.stdin.buffer.read().split()))
it = iter(data)
t = next(it)
out_lines = []
for _ in range(t):
n = next(it)
m = 2 * n
arr = [next(it) for _ in range(m)]
arr.sort()
max_sum = 0
for i in range(n):
s = arr[i] + arr[m - 1 - i]
if s > max_sum:
max_sum = s
out_lines.append(str(max_sum))
sys.stdout.write("\n".join(out_lines))
if __name__ == "__main__":
solve()
Explanation of the code:
Read all input numbers at once for speed.
For each test case, read n and the (2N) numbers.
Sort the list.
Iterate over the first (N) positions, compute the sum of the (i)-th smallest and (i)-th largest, and keep the maximum.