You are given an integer array elements. You can form a subsequence by deleting zero or more elements while preserving the relative order of the ones you keep.
Your goal is to return the maximum possible length of a subsequence where the count of adjacent pairs that have different values is at most k. Formally, for a chosen subsequence , the number of indices i satisfying and must not exceed k.
Constraints:
1 ≤ elements.length ≤ 10³1 ≤ k ≤ elements.length1 ≤ elements[i] ≤ 10⁹Example 1:
elements = [1, 1, 2, 3, 2, 1], k = 2
5
The longest valid subsequence is [1, 1, 2, 2, 1]. It contains exactly two unequal adjacent pairs: [1, 2] and [2, 1].
Example 2:
elements = [1, 2, 3, 3, 4, 3, 2, 2, 2, 1, 1], k = 2
8
Example 3:
elements = [5, 5, 5, 5, 5], k = 0
5
Example 1
Input:
6 2
1 1 2 3 2 1
Output: 5
Example 2
Input:
11 2
1 2 3 3 4 3 2 2 2 1 1
Output: 8