Candy Rush
InterviewTime limit2.5sMemory limit1024 MB
Find the longest contiguous block of candies that contains exactly K brands, with each brand appearing the same number of times.
- Level
Medium5 of 10
- Topics
- Sliding window, Hash map, Array
- Solved
- No attempts yet
Problem
It is rush hour! You have finished work and need to buy candies for every family member before the mall closes.
Your family values exclusivity and uniformity. Each family member must receive candies from a single brand, and no other member may receive candies from that brand. Each member must also receive the same number of candies, since you do not want to show a preference for anyone.
The mall sells candies from different brands, and your family has exactly members. This may look easy, but there is a catch.
The candies are displayed on one shelf. You do not have time to pick candies one by one, so you buy one contiguous block. If you buy two candies, you must also buy every candy between them on the shelf.
What is the maximum number of candies you can buy?
Input
The first line contains two integers and (). is the number of candies on the shelf, and is the number of family members. Brands are identified by distinct integers from 1 to .
The second line contains integers (), the brand of each candy in left-to-right order on the shelf.
Output
Print a single line with the maximum number of candies you can buy. No family member may receive two different brands, and no brand may go to two different members. Each member must receive the same number of candies, and the purchased candies must form one contiguous block on the shelf.