Question: Problem 4 Given are n real numbers a 1 , a 2 , . . . , an . Find a value t that can

Problem 4
Given are n real numbers a1, a2,..., an. Find a value t that can be obtained by adding two
of these numbers and, moreover, the number of pairs of these numbers that add up to t is the
largest possible. Formally, find a t such that the size of the set {(i, j)| i < j, ai + aj = t} is
maximum among all possible ts. For example, for 2,7,3,1,5,6, we have: 2+6=7+1=3+5.
This is the largest number of pairs that sum to the same number, and, therefore, t =8. If
there are multiple possible ts, find the smallest one. Give an O(n
2
log n) algorithm for this
problem.
Problem 4
Input specification: The input consists of two lines. The first line contains a positive integer n. The second line contains real numbers a1, a2,..., an, separated by spaces. You may assume that the real numbers fit in double. You may also assume that n is at least 2 and not larger than 10,000.
Output specification: The output contains a single line with two numbers: the first is the largest number of pairs that sums to the same number, and the second is the number t (i.e., the smallest sum with the largest number of corresponding pairs). Output t with precision of six digits after the decimal point.
Sample input: input-2.1, input-2.2, input-2.3, input-2.4, input-2.5, input-2.6, input-2.8, input-2.9
Sample output: answer-2.1, answer-2.2, answer-2.3, answer-2.4, answer-2.5, answer-2.6, answer-2.8, answer-2.9
How to submit: Name your files FindMaxPairsDouble.java or FindMaxPairsDouble.cpp

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!