GUPTA MECHANICAL

IN THIS WEBSITE I CAN TELL ALL ABOUT TECH. TIPS AND TRICKS APP REVIEWS AND UNBOXINGS ALSO TECH. NEWS .............

Wednesday 12 October 2022

[Solution] Yet Another Palindrome Problem CodeChef Solution



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.


Solution Click Below:-  👉CLICK HERE👈
👇👇👇👇👇

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

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]

No comments:

Post a Comment