Book 1-on-1 live assistance from the navigation menu
I'm just a message away. Let's solve it together. 👨💻
ZS Coding Question – Solved
12 Live
A class has students with various talents, each represented by an integer from 1 to talentsCount. You need to form teams for a quiz competition, where each team must have at least one member with each talent. Teams must be formed from consecutive students in the array. For each possible starting position, determine the minimum number of students needed to form a valid team. If it is not possible to form a team with all talents from a particular starting position, return -1 for that position.
Example
talentsCount = 3
talent = [1, 2, 3, 2, 1]
· Starting at position 1: [1, 2, 3] includes all talents, minimum size = 3
· Starting at position 2: [2, 3, 2, 1] is the smallest subarray with all talents, minimum size = 4
· Starting at position 3: [3, 2, 1] includes all talents, minimum size = 3
Starting at positions 4 and 5: Cannot form a team with all talents, return -1
The result [3, 4, 3, -1, -1].
Function Description
Complete the function teamSize in the editor with the following parameter(s):
int talent[n]: the students' talents
int talentsCount: the number of talents represented numbered from 1 to talentsCount
Returns
int[n]: at each index, the minimum size subarray required, or -1 if an appropriate subarray does not exist
Constraints
· 1 ≤ n, talentsCount ≤ 10^5
· 1 ≤ talent[i] ≤ talentsCount