#K80637. Equalize Array Elements

    ID: 35575 Type: Default 1000ms 256MiB

Equalize Array Elements

Equalize Array Elements

You are given an array of integers in each test case. In one operation, you can choose one distinct value and change all its occurrences to any other value already present in the array. Your task is to determine the minimum number of operations required to make all the elements in the array equal.

Formally, for a given array a of length n, let \( d \) be the number of distinct elements in a. It can be shown that the answer is \( d - 1 \). This is because in each operation you can eliminate one distinct value by converting all its occurrences to another value until only one distinct value remains.

Input/Output details:

  • Input: The input begins with an integer T representing the number of test cases. Each test case consists of a line with an integer n (the number of elements in the array) followed by a line containing n space-separated integers.
  • Output: For each test case, output a single integer on a new line, which is the minimum number of operations required to make all array elements equal.

inputFormat

The first line of input contains an integer T denoting the number of test cases. Each test case consists of two lines: the first line contains an integer n (the size of the array), and the second line contains n space-separated integers representing the elements of the array.

outputFormat

For each test case, output a single integer on a new line representing the minimum number of operations required to make all elements in the array equal.## sample

3
3
1 2 3
4
4 4 4 4
2
1 1
2

0 0

</p>