Mathison and the funny subarray

4.2

30 votes
Easy, Greedy Algorithms
Problem

Mathison has been playing quite a lot with some array A of length N. A subarray of the given array is called funny if it starts and ends with the same number.

Your task is to find the length of the longest funny subarray of the given array.

Input
The first line of the input file contains an integer, N, representing the number of integers in the given array.
The second line of the input file contains N space-separated integers, where the ith integer represents A[i] .

Output
The output file should contain only one integer, the length of the longest funny subarray.

Constraints

  • 1N106
  • 1A[i]105
  • A subarray is a subset of consecutive elements of an array.
Sample Input
9
2 2 3 1 2 2 3 1 1
Sample Output
6
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

The longest funny subarray has a length of 6. There are two possible choices: [2,2,3,1,2,2] and [1,2,2,3,1,1].

Editor Image

?