#C13376. Longest Disjoint Subsequence
Longest Disjoint Subsequence
Longest Disjoint Subsequence
Given an array of integers, your task is to find the longest subsequence such that the absolute difference between any two consecutive elements is strictly greater than 2. More formally, for a subsequence \(S = {s_1, s_2, \ldots, s_k}\), the following condition must hold:
\(s_{i+1} - s_i > 2 \quad \text{for all } 1 \leq i < k\)
You are required to implement a helper function valid_subsequence
which checks if a given subsequence satisfies the condition above, and then use it to determine the longest valid subsequence. The subsequence must be printed in increasing order. If multiple solutions exist, output any one of them.
If the input array is empty, output an empty line.
inputFormat
The input consists of two lines:
- The first line contains an integer \(N\) representing the number of elements in the array.
- The second line contains \(N\) space-separated integers.
outputFormat
Output the longest subsequence that satisfies the condition. The numbers should be printed on a single line, separated by a space. If the subsequence is empty, output an empty line.
## sample5
1 3 5 7 10
1 5 10