Boats Competition (Codeforces 1399C)
Tags: Greedy, two pointers

There are $$$n$$$ people who want to participate in a boat competition. The weight of the $$$i$$$-th participant is $$$w_i$$$. Only teams consisting of two people can participate in this competition. As an organizer, you think that it's fair to allow only teams with the same total weight.So, if there are $$$k$$$ teams $$$(a_1, b_1)$$$, $$$(a_2, b_2)$$$, $$$\dots$$$, $$$(a_k, b_k)$$$, where $$$a_i$$$ is the weight of the first participant of the $$$i$$$-th team and $$$b_i$$$ is the weight of the second participant of the $$$i$$$-th team, then the condition $$$a_1 + b_1 = a_2 + b_2 = \dots = a_k + b_k = s$$$, where $$$s$$$ is the total weight of each team, should be satisfied.Your task is to choose such $$$s$$$ that the number of teams people can create is the maximum possible. Note that each participant can be in no more than one team.You have to answer $$$t$$$ independent test cases.

Input: The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of test cases. Then $$$t$$$ test cases follow.The first line of the test case contains one integer $$$n$$$ ($$$1 \le n \le 50$$$) — the number of participants. The second line of the test case contains $$$n$$$ integers $$$w_1, w_2, \dots, w_n$$$ ($$$1 \le w_i \le n$$$), where $$$w_i$$$ is the weight of the $$$i$$$-th participant.

Output: For each test case, print one integer $$$k$$$: the maximum number of teams people can compose with the total weight $$$s$$$, if you choose $$$s$$$ optimally.

Note: In the first test case of the example, we can reach the optimal answer for $$$s=6$$$. Then the first boat is used by participants $$$1$$$ and $$$5$$$ and the second boat is used by participants $$$2$$$ and $$$4$$$ (indices are the same as weights).In the second test case of the example, we can reach the optimal answer for $$$s=12$$$. Then first $$$6$$$ participants can form $$$3$$$ pairs.In the third test case of the example, we can reach the optimal answer for $$$s=3$$$. The answer is $$$4$$$ because we have $$$4$$$ participants with weight $$$1$$$ and $$$4$$$ participants with weight $$$2$$$.In the fourth test case of the example, we can reach the optimal answer for $$$s=4$$$ or $$$s=6$$$.In the fifth test case of the example, we can reach the optimal answer for $$$s=3$$$. Note that participant with weight $$$3$$$ can't use the boat because there is no suitable pair for him in the list.

Sample Inputs:
5
5
1 2 3 4 5
8
6 6 6 6 6 6 8 8
8
1 2 2 1 2 1 1 2
3
1 3 3
6
1 1 3 4 2 2

Sample Outputs:
2
3
4
1
2

Solution (Python):
t = ''
for s in [*open(0)][2::2]:
    A = [0] * 51
    B = [0] * 101
    for x in map(int, s.split()):
        A[x] += 1
    for a in range(51):
        for b in range(a + 1, 51):
            B[a + b] += min(A[a], A[b])
    for i in range(51):
        B[2 * i] += A[i] // 2
    t += str(max(B)) + '\n'
print(t)

Solution (Python):
for _ in range(int(input())):
    n = int(input())
    arr = [int(x) for x in input().split()]
    d = {}
    for x in arr:
        d.setdefault(x, 0)
        d[x] += 1

    def give(k):
        grps = 0
        for i in range(k // 2 + 1):
            if i in d and k - i in d:
                if i != k - i:
                    grps += min(d[i], d[k - i])
                else:
                    grps += d[i] // 2
        return grps
    ans = 0
    for weight in range(2, 101):
        ans = max(ans, give(weight))
    print(ans)

Solution (Python):
def solve(n, arr):
    targets = [0 for i in range(101)]
    arr.sort()
    for tar in range(2, 101):
        (l, r) = (0, n - 1)
        while l < r:
            cursum = arr[l] + arr[r]
            if cursum == tar:
                targets[tar] += 1
                l += 1
                r -= 1
            elif cursum < tar:
                l += 1
            else:
                r -= 1
    print(max(targets))
for t in range(int(input())):
    n = int(input())
    arr = list(map(int, list(input().split())))
    solve(n, arr)