#C389. Longest Arithmetic Subarray

    ID: 47366 Type: Default 1000ms 256MiB

Longest Arithmetic Subarray

Longest Arithmetic Subarray

Given an array of integers, your task is to determine the length of the longest contiguous subarray that forms an arithmetic sequence.

An arithmetic sequence is one in which the difference between any two consecutive elements is constant. For example, in the array [2, 4, 6, 8, 10], the entire array forms an arithmetic sequence with a common difference of 2, so the answer is 5. In the array [1, 7, 4, 10, 13, 16, 19], the longest contiguous arithmetic subarray is [7, 4, 10, 13] (with a constant difference of -3, 6, 3, etc., note that the arithmetic sequence must be contiguous and maintain a constant difference), hence the output is 4. This problem tests your understanding of array traversal and maintaining state for contiguous subsequences.

inputFormat

The input is read from standard input (stdin). The first line contains an integer n (1 ≤ n ≤ 10^5), the number of elements in the array. The second line contains n space-separated integers, each in the range [-10^9, 10^9].

outputFormat

Output a single integer to standard output (stdout) which represents the length of the longest contiguous subarray that forms an arithmetic sequence.## sample

5
2 4 6 8 10
5