Yet Another Palindrome Problem Codechef Solution

We Are Discuss About CODECHEF SOLUTION

Yet Another Palindrome Problem Codechef Solution

Yet Another Palindrome Problem Codechef Solution

Answers will be Uploaded Shortly and it will be Notified on Telegram, So JOIN NOW
JoinScishowEngineerTelegram

Problem

Chef has an array A of size N. He can perform the following operation on A:

  • Select an i (1 \le i \le N) and for all 1 \le j \le i, set A_j := A_j + 1 (i.e. add 1 to every element in the prefix of length i).

Chef wants to convert A to a palindrome by using the above operation minimum number of times. Can you help Chef?
If it is not possible to convert A to a palindrome, output -1.

Note: An array is called palindrome if it reads the same backwards and forwards, for e.g. [1, 4, 1] and [7, 3, 3, 7] are palindromic arrays.

Input Format

  • The first line contains a single integer T — the number of test cases. Then the test cases follow.
  • The first line of each test case contains an integer N — the size of the array A.
  • The second line of each test case contains N space-separated integers A_1, A_2, \dots, A_N denoting the array A.

Output Format

For each test case, output the minimum number of operations required to convert A to a palindromic array.

If it is not possible to do so, output -1.

Constraints

  • 1 \leq T \leq 10^5
  • 1 \leq N \leq 3 \cdot 10^5
  • 1 \le A_i \le 10^9
  • Sum of N over all test cases does not exceed 3 \cdot 10^5.

Sample 1:

Input

Output

3
4
4 2 2 4
5
5 4 3 2 1
4
1 2 3 4
0
-1
3

Explanation:

Test case 1: The given array is already a palindrome.

Test case 2: It can be proven that it is not possible to convert A to a palindromic array using the given operation.

Test case 3: We can perform the following operations:

  • Select i = 1[1, 2, 3, 4] \rightarrow [2, 2, 3, 4]
  • Select i = 2[2, 2, 3, 4] \rightarrow [3, 3, 3, 4]
  • Select i = 1[3, 3, 3, 4] \rightarrow [4, 3, 3, 4]
Answers will be Uploaded Shortly and it will be Notified on Telegram, So JOIN NOW
JoinScishowEngineerTelegram

 

SOLUTION

Yet Another Palindrome Problem Codechef Solution

Yhaa You have done it but next? if YOU Want to Get Others Please Visit Here JOIN NOW

Answers will be Uploaded Shortly and it will be Notified on Telegram, So JOIN NOW
JoinScishowEngineerTelegram

Related Posts

Leave a Reply

Your email address will not be published.

adplus-dvertising