Ctrl K

Gift a Stretch, Keep the Flavors

Medium
Candies lie in a row and flavors[i] is the flavor of candy i. You must give a friend exactly gift candies that lie next to each other and keep the rest. You want to be left with as many different flavors as possible. Return the largest number of distinct flavors among the candies you keep.
Example 1
flavors=[1, 3, 3, 2, 1], gift=2 → 3

Give away the two 3s (positions 1 and 2). That leaves 1, 2, 1: two flavors. No other gift leaves more.

Example 2
flavors=[4, 5, 6, 4], gift=1 → 3

Give away one of the 4s, keeping flavors 4, 5 and 6.

Example 3
flavors=[7, 8], gift=0 → 2

Nothing has to be given, so both flavors stay.

Constraints
  • 0 ≤ len(flavors) ≤ 10⁵
  • 1 ≤ flavors[i] ≤ 10⁵
  • 0 ≤ gift ≤ len(flavors)
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTwo-Genre Stretch of a Playlist MediumWindows With an Exact Alarm Count MediumStretches With k Odd Numbers MediumTwo Separate Segments With a Given Sum MediumTrim the Ends to a Target Medium
loading Python…
Input
flavors=[1, 3, 3, 2, 1], gift=2
Expected
3

Run checks the first 3 tests in your browser. Submit checks all 112 tests on the server, hidden ones included.