There are 𝑛 people living in the town. Just now, the wealth of the 𝑖 -th person was…
- User
There are 𝑛 people living in the town. Just now, the wealth of the 𝑖 -th person was 𝑎𝑖 gold. But guess what? The richest person has found an extra pot of gold!
More formally, find an 𝑎𝑗=𝑚𝑎𝑥(𝑎1,𝑎2,…,𝑎𝑛) , change 𝑎𝑗 to 𝑎𝑗+𝑥 , where 𝑥 is a non-negative integer number of gold found in the pot. If there are multiple maxima, it can be any one of them.
A person is unhappy if their wealth is strictly less than half of the average wealth∗ .
If strictly more than half of the total population 𝑛 are unhappy, Robin Hood will appear by popular demand.
Determine the minimum value of 𝑥 for Robin Hood to appear, or output −1 if it is impossible.
∗ The average wealth is defined as the total wealth divided by the total population 𝑛 , that is, ∑𝑎𝑖𝑛 , the result is a real number.
Input The first line of input contains one integer 𝑡 (1≤𝑡≤104 ) — the number of test cases.
The first line of each test case contains an integer 𝑛 (1≤𝑛≤2⋅105 ) — the total population.
The second line of each test case contains 𝑛 integers 𝑎1,𝑎2,…,𝑎𝑛 (1≤𝑎𝑖≤106 ) — the wealth of each person.
It is guaranteed that the sum of 𝑛 across all test cases does not exceed 2⋅105 .
Output For each test case, output one integer — the minimum number of gold that the richest person must find for Robin Hood to appear. If it is impossible, output −1 instead.
run binary search on answer to minimize number of gold that richest person must find, and all calculations should be calculated as double (real numbers). give solution in c++
- Assistant