Constant Palindrome Sum
Contest: Codeforces Round 636 (Div. 3)
Difficulty rating: 1700
Tags: brute force, data structures, greedy, two pointers

You are given an array $$$a$$$ consisting of $$$n$$$ integers (it is guaranteed that $$$n$$$ is even, i.e. divisible by $$$2$$$). All $$$a_i$$$ does not exceed some integer $$$k$$$.

Your task is to replace the minimum number of elements (replacement is the following operation: choose some index $$$i$$$ from $$$1$$$ to $$$n$$$ and replace $$$a_i$$$ with some integer in range $$$[1; k]$$$) to satisfy the following conditions:

- after all replacements, all $$$a_i$$$ are positive integers not greater than $$$k$$$;
- for all $$$i$$$ from $$$1$$$ to $$$\frac{n}{2}$$$ the following equation is true: $$$a_i + a_{n - i + 1} = x$$$, where $$$x$$$ should be the same for all $$$\frac{n}{2}$$$ pairs of elements.

You have to answer $$$t$$$ independent test cases.

Input format:
The first line of the input contains one integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Then $$$t$$$ test cases follow.

The first line of the test case contains two integers $$$n$$$ and $$$k$$$ ($$$2 \le n \le 2 \cdot 10^5, 1 \le k \le 2 \cdot 10^5$$$) — the length of $$$a$$$ and the maximum possible value of some $$$a_i$$$ correspondingly. It is guratanteed that $$$n$$$ is even (i.e. divisible by $$$2$$$). The second line of the test case contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$1 \le a_i \le k$$$), where $$$a_i$$$ is the $$$i$$$-th element of $$$a$$$.

It is guaranteed that the sum of $$$n$$$ (as well as the sum of $$$k$$$) over all test cases does not exceed $$$2 \cdot 10^5$$$ ($$$\sum n \le 2 \cdot 10^5$$$, $$$\sum k \le 2 \cdot 10^5$$$).

Output format:
For each test case, print the answer — the minimum number of elements you have to replace in $$$a$$$ to satisfy the conditions from the problem statement.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Example input:
4
4 2
1 2 1 2
4 3
1 2 2 1
8 7
6 1 1 7 6 3 4 6
6 6
5 2 6 1 3 4

Example output:
0
1
4
2

Editorial:
It is obvious that if we fix the value of $$$x$$$ then there are three cases for the pair of elements:
1. We don't need to change anything in this pair;
2. we can replace one element to fix this pair;
3. we need to replace both elements to fix this pair.
The first part can be calculated easily in $$$O(n+k)$$$, we just need to create the array of frequencies $$$cnt$$$, where $$$cnt_x$$$ is the number of such pairs $$$(a_i, a_{n-i+1})$$$ that $$$a_i + a_{n-i+1} = x$$$.
The second part is a bit tricky but still doable in $$$O(n+k)$$$. For each pair, let's understand the minimum and the maximum sum we can obtain using at most one replacement. For the $$$i$$$-th pair, all such sums belong to the segment $$$[min(a_i, a_{n-i+1})+1; max(a_i, a_{n-i+1})+k]$$$. Let's make $$$+1$$$ on this segment using prefix sums (make $$$+1$$$ in the left border, $$$-1$$$ in the right border plus one and then just compute prefix sums on this array). Let this array be $$$pref$$$. Then the value $$$pref_x$$$ tells the number of such pairs that we need to replace at most one element in this pair to make it sum equals $$$x$$$.
And the last part can be calculated as $$$\frac{n}{2} - pref_x$$$. So, for the sum $$$x$$$ the answer is $$$(pref_x - cnt_x) + (\frac{n}{2} - pref_x) \cdot 2$$$. We just need to take the minimum such value among all possible sums from $$$2$$$ to $$$2k$$$.
There is another one solution that uses scanline, not depends on $$$k$$$ and works in $$$O(n \log n)$$$ but it has no cool ideas to explain it here (anyway the main idea is almost the same as in the solution above).