#K47462. Maximum Length Subsequence

    ID: 28204 Type: Default 1000ms 256MiB

Maximum Length Subsequence

Maximum Length Subsequence

Given an array of integers, find the maximum length of a subsequence such that the absolute difference between any two consecutive elements is at most \(1\). The subsequence does not need to be contiguous; you can select elements from the array in any order. Formally, for an array \(A\) of \(n\) integers, determine the maximum value of \(L\) for which there exists a subsequence \(S\) of \(A\) satisfying \(|S_i - S_{i+1}| \leq 1\) for all \(1 \leq i < L\).

inputFormat

The input consists of two lines. The first line contains an integer \(n\) \( (1 \leq n \leq 10^5)\) denoting the number of elements in the array. The second line contains \(n\) space‐separated integers representing the elements of the array.

outputFormat

Output a single integer — the maximum length of the subsequence satisfying the condition.

## sample
6
1 2 2 3 1 2
5