#K69197. Longest Subarray with Matching Endpoints

    ID: 33033 Type: Default 1000ms 256MiB

Longest Subarray with Matching Endpoints

Longest Subarray with Matching Endpoints

Given an integer \(N\) and an array of \(N\) integers, find the length of the longest contiguous subarray such that the first and last elements of the subarray are identical. In other words, for a subarray \(A[l..r]\), it must hold that \(A[l]=A[r]\). This problem tests your ability to efficiently track indices and compute subarray lengths.

The key idea is to record the first occurrence of each number and then, as you traverse the array, update the maximum length of any subarray that meets the condition.

inputFormat

The first line of input contains an integer \(N\), the number of elements in the array. The second line contains \(N\) space-separated integers representing the array elements.

outputFormat

Output a single integer which is the length of the longest contiguous subarray that starts and ends with the same element.

## sample
5
1 2 3 1 2
4