codeforces-scrape

Codeforces scraped problems and editorials
git clone git://git.laack.co/codeforces-scrape.git
Log | Files | Refs | README

1610B.md (2271B)


      1 Source: https://codeforces.com/problemset/problem/1610/B
      2 
      3 B. Kalindrome Array
      4 
      5 time limit per test
      6 
      7 1 second
      8 
      9 memory limit per test
     10 
     11 256 megabytes
     12 
     13 input
     14 
     15 standard input
     16 
     17 output
     18 
     19 standard output
     20 
     21 An array $[b_1, b_2, \ldots, b_m]$ is a palindrome, if $b_i = b_{m+1-i}$ for each $i$ from $1$ to $m$. Empty array is also a palindrome.
     22 
     23 An array is called kalindrome, if the following condition holds:
     24 
     25 * It's possible to select some integer $x$ and delete some of the elements of the array equal to $x$, so that the remaining array (after gluing together the remaining parts) is a palindrome.
     26 
     27 Note that you don't have to delete all elements equal to $x$, and you don't have to delete at least one element equal to $x$.
     28 
     29 For example :
     30 
     31 * $[1, 2, 1]$ is kalindrome because you can simply not delete a single element.
     32 * $[3, 1, 2, 3, 1]$ is kalindrome because you can choose $x = 3$ and delete both elements equal to $3$, obtaining array $[1, 2, 1]$, which is a palindrome.
     33 * $[1, 2, 3]$ is not kalindrome.
     34 
     35 You are given an array $[a_1, a_2, \ldots, a_n]$. Determine if $a$ is kalindrome or not.
     36 
     37 Input
     38 
     39 The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The description of the test cases follows.
     40 
     41 The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$) — the length of the array.
     42 
     43 The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($1 \le a_i \le n$) — elements of the array.
     44 
     45 It's guaranteed that the sum of $n$ over all test cases won't exceed $2 \cdot 10^5$.
     46 
     47 Output
     48 
     49 For each test case, print YES if $a$ is kalindrome and NO otherwise. You can print each letter in any case.
     50 
     51 Example
     52 
     53 Input
     54 
     55 ```
     56 4
     57 1
     58 1
     59 2
     60 1 2
     61 3
     62 1 2 3
     63 5
     64 1 4 4 1 4
     65 ```
     66 
     67 Output
     68 
     69 ```
     70 YES
     71 YES
     72 NO
     73 YES
     74 ```
     75 
     76 Note
     77 
     78 In the first test case, array $[1]$ is already a palindrome, so it's a kalindrome as well.
     79 
     80 In the second test case, we can choose $x = 2$, delete the second element, and obtain array $[1]$, which is a palindrome.
     81 
     82 In the third test case, it's impossible to obtain a palindrome.
     83 
     84 In the fourth test case, you can choose $x = 4$ and delete the fifth element, obtaining $[1, 4, 4, 1]$. You also can choose $x = 1$, delete the first and the fourth elements, and obtain $[4, 4, 4]$.