CF BUDDY
← Problems·

1626D · Martial Arts Tournament

2100 · binary search, brute force, greedy

Problem: Monocarp is planning to host a martial arts tournament. There will be three divisions based on weight: lightweight, middleweight and heavyweight. The winner of each division will be determined by a single elimination system.

In particular, that implies that the number of participants in each division should be a power of two. Additionally, each division should have a non-zero amount of participants.

nn participants have registered for the tournament so far, the ii-th of them weighs aia_i. To split participants into divisions, Monocarp is going to establish two integer weight boundaries xx and yy (x<yx < y).

All participants who weigh strictly less than xx will be considered lightweight. All participants who weigh greater or equal to yy will be considered heavyweight. The remaining participants will be considered middleweight.

It's possible that the distribution doesn't make the number of participants in each division a power of two. It can also lead to empty divisions. To fix the issues, Monocarp can invite an arbitrary number of participants to each division.

Note that Monocarp can't kick out any of the nn participants who have already registered for the tournament.

However, he wants to invite as little extra participants as possible. Help Monocarp to choose xx and yy in such a way that the total amount of extra participants required is as small as possible. Output that amount.

Input Format: The first line contains a single integer tt (1t1041 \le t \le 10^4) — the number of testcases.

The first line of each testcase contains a single integer nn (1n21051 \le n \le 2 \cdot 10^5) — the number of the registered participants.

The second line of each testcase contains nn integers a1,a2,,ana_1, a_2, \dots, a_n (1ain1 \le a_i \le n) — the weights of the registered participants.

The sum of nn over all testcases doesn't exceed 21052 \cdot 10^5.

Output Format: For each testcase, print a single integer — the smallest number of extra participants Monocarp is required to invite after he chooses the weight boundaries xx and yy.

Note: In the first testcase of the example, Monocarp can choose x=2x=2 and y=3y=3. Lightweight, middleweight and heavyweight divisions will have 22, 11 and 11 participants, respectively. They all are powers of two, so no extra participants are required.

In the second testcase of the example, regardless of the choice of xx and yy, one division will have 11 participant, the rest will have 00. Thus, Monocarp will have to invite 11 participant into both of the remaining divisions.

In the third testcase of the example, Monocarp can choose x=1x=1 and y=2y=2. Lightweight, middleweight and heavyweight divisions will have 00, 33 and 33 participants, respectively. So an extra participant is needed in each division.

In the fourth testcase of the example, Monocarp can choose x=8x=8 and y=9y=9. Lightweight, middleweight and heavyweight divisions will have 88, 00 and 00 participants, respectively. Middleweight and heavyweight division need an extra participant each.

Sample Cases

Case 1

Input

4
4
3 1 2 1
1
1
6
2 2 2 1 1 1
8
6 3 6 3 6 3 6 6

Output

0
2
3
2

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback