#C1387. Longest Magical Sequence
Longest Magical Sequence
Longest Magical Sequence
You are given an array of integers. Your task is to find the length of the longest contiguous subarray (also called a sequence) where every pair of adjacent elements differs by exactly 1. In other words, for any two consecutive elements \(a_i\) and \(a_{i+1}\) in the subarray, the condition \(|a_i - a_{i+1}| = 1\) must hold.
Example:
- For the array [1, 2, 3, 2, 1], the entire array is a magical sequence since \(|1-2|=1, |2-3|=1, |3-2|=1, |2-1|=1\), so the answer is 5.
- For the array [1, 2, 4, 5], the longest magical subarray is either [1, 2] or [4, 5] with length 2.
You need to process multiple test cases. For each test case, output the length of the longest magical sequence found.
inputFormat
The input is read from standard input (stdin) and has the following format:
T N1 a11 a12 ... a1N1 N2 a21 a22 ... a2N2 ... NT aT1 aT2 ... aTNT
Where:
- T is the number of test cases.
- For each test case, the first line contains an integer N (the number of elements in the array) and the second line contains N space-separated integers.
outputFormat
For each test case, print a single integer denoting the length of the longest contiguous subarray that is a Magical Sequence. Each result should be printed on a new line.
## sample3
5
1 2 3 2 1
4
1 2 4 5
6
7 6 5 6 7 8
5
2
6
</p>