#K52402. Good Sequence Checker

    ID: 29301 Type: Default 1000ms 256MiB

Good Sequence Checker

Good Sequence Checker

You are given a sequence of integers. A sequence is called good if it can be transformed into a sequence with all equal elements by performing a series of operations. The allowed operations are:

  • A right circular shift (i.e. move the last element to the front).
  • Reversing the sequence.
  • Again, performing right circular shifts.

After analyzing these operations, it turns out that a necessary and sufficient condition for the sequence to be good is that the number of distinct elements in the sequence is at most 2. In other words, if we denote by \(|S|\) the number of unique elements in a sequence \(S\), then the sequence is good if and only if \[ |S| \le 2. \]

Your task is to check several sequences and determine for each sequence whether it is good. Print "YES" if the sequence is good and "NO" otherwise.

inputFormat

The input is given from standard input. The first line contains an integer \(q\) representing the number of test cases. Each of the next \(q\) lines begins with an integer \(n\) which indicates the number of elements in the sequence, followed by \(n\) integers representing the sequence.

For example:

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

outputFormat

For each test case, output a single line containing "YES" if the sequence is good, or "NO" otherwise.

For the sample input above, the correct output is:

NO
YES
YES
## sample
3
4 1 2 3 4
3 1 1 1
5 2 4 4 2 2
NO

YES YES

</p>