#C11124. Interesting Queue

    ID: 40406 Type: Default 1000ms 256MiB

Interesting Queue

Interesting Queue

You are given a queue of N people, where the height of the i-th person in the queue is given by A[i]. A queue is considered interesting if for every consecutive pair of people, the absolute difference of their heights is at most 1, i.e. for every i (1 ≤ i < N),

\(|A[i] - A[i+1]| \le 1\)

Your task is to determine whether the given queue is interesting. If it is, output 0 (indicating no operations are needed); otherwise, output -1 (indicating it is impossible to make the queue interesting by any allowed operations).

Note: In this problem, you are not allowed to change the order of the queue. You only need to check the current configuration. The only valid outputs are 0 if the queue is already interesting, and -1 otherwise.

inputFormat

The first line contains an integer T (T ≥ 1) representing the number of test cases.

Each test case consists of two lines:

  • The first line contains an integer N (1 ≤ N ≤ 105), the number of people in the queue.
  • The second line contains N space-separated integers A[0] A[1] ... A[N-1], where A[i] represents the height of the i-th person.

The sum of N over all test cases does not exceed 106.

outputFormat

For each test case, output a single line containing a single integer: 0 if the queue is interesting (i.e. |A[i]-A[i+1]| ≤ 1 for every adjacent pair), or -1 if it is not.

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

-1

</p>