题目描述
Given an array of size n. The task is to find the longest subsequence such that absolute difference between adjacents is one.
Example 1:
Input : Arr[] = {10, 9, 4, 5, 4, 8, 6}
Output : 3
Explanation:
As longest subsequences with difference 1
are, ("10, 9, 8"), ("4, 5, 4") and
("4, 5, 6").
Example 2:
Input : Arr[] = {1, 2, 3, 2, 3, 7, 2, 1}
Output : 7
Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function longLenSub() that takes an array (arr), sizeOfArray (n), and return the length of the longest subsequence with absolute difference 1. The driver code takes care of the printing.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 ≤ n, a[i] ≤ 10^{5}
输入格式
见题面
输出格式
见题面