#K67662. Longest Subsequence with Consecutive Difference One

    ID: 32693 Type: Default 1000ms 256MiB

Longest Subsequence with Consecutive Difference One

Longest Subsequence with Consecutive Difference One

You are given an array of (N) integers. Your task is to find the length of the longest subsequence such that the difference between every two consecutive elements in the subsequence is exactly one. A subsequence is obtained by removing some elements (possibly none) from the original array without changing the order of the remaining elements.

For example, given the array [1, 2, 3, 4, 5, 3, 2], one of the longest valid subsequences is [1, 2, 3, 4, 5] which has a length of 5. Use (\LaTeX) formatting for any mathematical expressions if needed.

inputFormat

The input consists of two lines:
1. The first line contains an integer (N), the number of elements in the array.
2. The second line contains (N) space-separated integers representing the array elements. If (N = 0), the second line will be empty.

outputFormat

Output a single integer: the length of the longest subsequence in which the difference between every two consecutive elements is exactly one.## sample

7
1 2 3 4 5 3 2
5