#C10632. Longest Constant-Difference Subsequence

    ID: 39859 Type: Default 1000ms 256MiB

Longest Constant-Difference Subsequence

Longest Constant-Difference Subsequence

Given a sequence of n integers, your task is to determine the length of the longest contiguous subsequence (segment) in which the difference between consecutive elements is constant. In other words, for a contiguous subsequence \(a_1, a_2, \ldots, a_k\), there exists a constant \(d\) such that \(a_{i+1} - a_i = d\) for all \(1 \le i < k\).

The input starts with an integer representing the number of test cases. For each test case, you are given an integer n followed by n space-separated integers. For each test case, output the maximum length of such a subsequence.

Note: A sequence with only two numbers always forms a constant-difference subsequence.

inputFormat

The input is read from standard input (stdin) and is structured as follows:

  • The first line contains an integer T denoting the number of test cases.
  • For each test case:
    • The first line contains an integer n, the number of elements in the sequence.
    • The next line (or following space-separated values) contains n integers representing the sequence.

outputFormat

For each test case, output a single line showing the length of the longest contiguous subsequence with a constant difference between adjacent elements. The output is printed to standard output (stdout).

## sample
3
5
1 3 5 7 9
6
10 7 4 3 0 -3
4
1 2 4 6
5

3 3

</p>