2. Unequal Elements
A test needs to be prepared on the HackerRank platform with questions from different sets of skills to assess candidates. Given an array, skills, of size n, where skills[i] denotes the skill type of the ith question, select skills for the questions on the test. The skills should be grouped together as much as possible. The goal is to find the maximum length of a subsequence of skills such that there are no more than k unequal adjacent elements in the subsequence. Formally, find a subsequence of skills, call it x, of length m such that there are at most k indices where x[i] != x[i+1] for all 0 ? i < m.
Note: A subsequence of an array is obtained by deleting several elements of the array (possibly zero or all) without changing the order of the remaining elements. For example, [1, 3, 4], [3], and [] are subsequences of [1, 2, 3, 4] whereas [1, 5], [4, 3] are not.
Example
skills = [1, 1, 2, 3, 2, 1], k = 2
The longest possible subsequence is x = [1, 1, 2, 2, 1]. There are only two indices where x[j] != x[j+1] and x[3] != x[4].
Return its length, 5.
Function Description
Complete the function findMaxLength in the editor below.
findMaxLength has the following parameter(s):
int skills[n]: the different skill types
int k: the maximum count of unequal adjacent elements
Returns
int: the maximum value of m
Constraints
1 ? n ? 2 * 10^3
1 ? k ? n
1 ? skills[i] ? 2 * 10^3