CF2112B.Shrinking Array

传统题 时间 2000 ms 内存 256 MiB 5 尝试 1 已通过 1 标签

Shrinking Array

Let's call an array bb beautiful if it consists of at least two elements and there exists a position ii such that bibi+11|b_i - b_{i + 1}| \le 1 (where x|x| is the absolute value of xx).

You are given an array aa, and as long as it consists of at least two elements, you can perform the following operation:

  1. Choose two adjacent positions ii and i+1i + 1 in the array aa.
  2. Choose an integer xx such that $\min(a_i, a_{i + 1}) \le x \le \max(a_i, a_{i + 1})$.
  3. Remove the numbers aia_i and ai+1a_{i + 1} from the array, and insert the number xx in their place. Obviously, the size of the array will decrease by 11.

Calculate the minimum number of operations required to make the array beautiful, or report that it is impossible.

Input

The first line contains one integer tt (1t2001 \le t \le 200) — the number of test cases.

The first line of each test case contains one integer nn (2n10002 \le n \le 1000) — the size of the array aa.

The second line contains nn integers a1,a2,,ana_1, a_2, \dots, a_n (1ai1061 \le a_i \le 10^6) — the array aa itself.

Output

For each test case, output one integer — the minimum number of operations needed to make the array aa beautiful, or 1-1 if it is impossible to make it beautiful.

Note

In the first test case, the given array is already beautiful, as a2a3=33=0|a_2 - a_3| = |3 - 3| = 0.

In the second test case, it is impossible to make the array beautiful, as applying the operation would reduce its size to less than two.

In the third test case, you can, for example, choose a1a_1 and a2a_2 and replace them with the number 22. The resulting array [2,3,7][2, 3, 7] is beautiful.

In the fourth test case, you can, for example, choose a2a_2 and a3a_3 and replace them with the number 33. The resulting array [1,3,2][1, 3, 2] is beautiful.

Samples

4
4
1 3 3 7
2
6 9
4
3 1 3 7
4
1 3 5 2
0
-1
1
1

在线编程 IDE

建议全屏模式获得最佳体验