6

Practice problems #

Question 1 - “Non-Fancy Trains” #

Problem Description #

There are \(n\) stations numbered \(1, 2, ... n\) . Stations \(i\) and \(j\) are connected via a train ( \(1 \le i, j \le n\) ) iff \(|i - j| \le 2\) . The price of such a train would be \(|a_i - a_{j}|\) where \(a_i\) is an input array denoting the ratings of stations. If you travel optimally, find the minimum cost to travel form station \(1\) to station \(n\) .

Input Format #

The first line of input contains a single integer \(n\) denoting the number of stations.

The second line contains \(n\) space-seperated integers, \(a_i\) .

Input constraints #

  • \(2 \le n \le 10^5\)
  • \(1 \le a_i \le 10^4\)

Output Format #

Print one integer, the minimum cost to travel from station \(1\) to station \(n\) .

Sample Input 1 #

4
10 30 40 20

Sample Output 1 #

30

Sample Explanation #

One valid path is to go through the stations \(1 \rightarrow 2 \rightarrow 4\) , which incurs the cost \(|10 - 30| + |30 - 20| = 30\)

**Sample Input 2 **

2
10 10

Sample Output 2

0

Question 2 - “String Palindrome Check” #

Problem Description #

Given a string \(S\) , check if it is a palindrome using recursion.

Input Format #

The first line of input contains a single integer \(2T\) that denotes the number of test-cases. Then, \(2T\) lines follow. The first line of each test-case contains a single integer \(N\) denoting the length of the string. The second line of each test-case contains a string \(S\) of length \(N\) .

Input constraints #

  • \(1 \le T \le 2 \times 10^5\)
  • \(1 \le N \le 2 \times 10^5\)
  • \(|S| = N\)
  • \(S\) consists of only lowercase English alphabets
  • The sum of \(N\) over all test-cases does not exceed \(2 \times 10^5\)

Output Format #

For each test-case, on a single line, output YES if the string is a palindrome and NO if not

Sample inputs and outputs #

Sample Input 1

2
addd
sss

Sample Output 1

NO
YES

Sample Input 2

3
addfdfdda
a
addfdffdda

Sample Output 2

YES
YES
NO