Problem · Dynamic Programming

Unequal Elements (Also for Core/Database Intern :)

Learn this problem
HardSnowflakeINTERNOA
See Snowflake hiring insights

Problem statement

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

findMaxLength(skills: int[], k: int) → int

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🌷 ✩࿐⋆*

Examples

Example 1

skills = [1, 1, 2, 3, 2, 1]k = 2return = 5
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

  • 1 ≤ n ≤ 2 * 103
  • 1 ≤ k < n
  • 1 ≤ skills[i] ≤ 2 * 103

More Snowflake problems

drafts saved locally
public int findMaxLength(int[] skills, int k) {
  // write your code here
}
skills[1, 1, 2, 3, 2, 1]
k2
expected5
checking account