FastPrepFastPrep
Problem Brief

Unequal Elements (Also for Core/Database Intern :)

INTERNOA
See Snowflake online assessment and hiring insights

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.

Function Description

Complete the function findMaxLength in the editor.

findMaxLength has the following parameters:

  1. 1. int skills[n]: the different skill types
  2. 2. int k: the maximum count of unequal adjacent elements

Returns

int: the maximum value of m

ᥫ᭡.🌷Credit to robot🌷 ✩࿐⋆*

1Example 1

Input
skills = [1, 1, 2, 3, 2, 1], k = 2
Output
5
Explanation
The longest possible subsequence is x = [1, 1, 2, 2, 1]. There are only two indices where x[1] != x[2] and x[3] != x[4]. Return its length, 5.

Constraints

Limits and guarantees your solution can rely on.

  • 1 ≤ n ≤ 2 * 103
  • 1 ≤ k < n
  • 1 ≤ skills[i] ≤ 2 * 103
public int findMaxLength(int[] skills, int k) {
  // write your code here
}
Input

skills

[1, 1, 2, 3, 2, 1]

k

2

Output

5

Sign in to submit your solution.