#K86587. Minimum Removals to Ensure No Adjacent Same Height Flowers

    ID: 36897 Type: Default 1000ms 256MiB

Minimum Removals to Ensure No Adjacent Same Height Flowers

Minimum Removals to Ensure No Adjacent Same Height Flowers

You are given a sequence representing the heights of flowers arranged in a row. Your task is to determine the minimum number of flowers that need to be removed so that no two adjacent flowers have the same height.

For each test case, the first line contains an integer N indicating the number of flowers, followed by a line of N integers that represent the heights of the flowers. Removing a flower means it is no longer considered in the adjacent sequence. Note that you are not allowed to reorder the remaining flowers.

Input Format:

  • The first line contains an integer T representing the number of test cases.
  • For each test case:
    • First, an integer N (number of flowers).
    • Next, a line containing N space-separated integers representing the heights of the flowers.

Output Format:

  • For each test case, output a single integer on a new line denoting the minimum number of removals required.

Formula:

For a given array of heights \(h_1, h_2, \ldots, h_N\), the answer is computed by:

\[ \text{removals} = \sum_{i=2}^{N} \mathbf{1}_{\{h_i = h_{i-1}\}} \]

inputFormat

The input is read from stdin and is structured as follows:

T
N_1
h_{1,1} h_{1,2} ... h_{1,N1}
N_2
h_{2,1} h_{2,2} ... h_{2,N2}
...
N_T
h_{T,1} h_{T,2} ... h_{T,NT}

Where T is the number of test cases and for each test case, N is the number of flowers followed by a line of N space-separated integers representing their heights.

outputFormat

For each test case, output a single integer on a new line denoting the minimum number of flowers that need to be removed so that no two adjacent flowers have the same height. The output is written to stdout.

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

0 0 4 3

</p>