#K50732. Minimum Cost for Equal Tree Heights

    ID: 28930 Type: Default 1000ms 256MiB

Minimum Cost for Equal Tree Heights

Minimum Cost for Equal Tree Heights

You are given T test cases. In each test case, you are given an integer n representing the number of trees and a list of n integers representing the heights of the trees.

Your task is to determine the minimum cost (in terms of the number of trees to remove) so that all the remaining trees have the same height. Note that you are allowed to remove trees, and if needed, you may remove all of them.

Formally, for each test case, you need to compute the value:

\( \text{cost} = n - \max_{h}(\text{frequency}(h)) \)

where \( \max_{h}(\text{frequency}(h)) \) is the maximum frequency among the different heights in the test case.

Example: For a test case with n = 3 and tree heights [2, 2, 3], the maximum frequency is 2 (for height 2), so the minimum cost is \(3 - 2 = 1\).

Make sure your program reads input from standard input (stdin) and prints the output to standard output (stdout).

inputFormat

The first line of the input contains an integer T (\(1 \le T \le 10^5\)), the number of test cases. Each test case is described as follows:

  • The first line contains an integer n (\(1 \le n \le 10^5\)), the number of trees.
  • The second line contains n space-separated integers representing the heights of the trees. Each height is an integer between 1 and \(10^9\).

It is guaranteed that the sum of all n does not exceed \(10^5\).

outputFormat

For each test case, output a single integer — the minimum number of trees that need to be removed so that the remaining trees all have the same height. Each answer should be printed on a new line.

## sample
4
3
2 2 3
4
1 3 3 2
5
4 5 5 5 6
2
1 1000000000
1

2 2 1

</p>