All problems

Problem 371

Longest Harmonious Subsequence

Easy · 10 pts · Array, Hash Table, Sorting

Problem

A harmonious sequence is one where the difference between its maximum and its minimum is exactly 1. Find the length of the longest harmonious subsequence of the array; a subsequence keeps some of the elements, in their original order, and may skip others. Since only the values matter, the answer is the largest count(v) + count(v + 1) over values v where both v and v + 1 appear. Print 0 if there is no harmonious subsequence.

Input

First line: N. Second line: N integers.

Output

Print the length of the longest harmonious subsequence.

Constraints

1 <= N <= 100000; -1000000000 <= A[i] <= 1000000000

Example input

8
1 3 2 2 5 2 3 7

Example output

5

Explanation

In 1 3 2 2 5 2 3 7, taking all the 2s and 3s gives 3 2 2 2 3, whose maximum and minimum differ by exactly 1. Its length is 5.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Interval List Intersections Next: H-Index