Double Rainbow
InterviewTime limit1sMemory limit1024 MB
Given a color sequence and k colors, find the shortest contiguous block that contains every color and whose complement also contains every color, or print 0.
- Level
Medium6 of 10
- Topics
- Two pointers, Sliding window, Prefix sum, Array
- Solved
- No attempts yet
Problem
Let be a set of points on the -axis, and each point is colored with one of the colors . For each of the colors, there is at least one point in colored with that color. For a set of consecutive points from , if both and contain at least one point of each color, then makes a double rainbow. See the figure below as an example. The set consists of ten points, and each point is colored with one of the colors , , , and . The set of the five consecutive points inside the rectangle makes a double rainbow.

Given a set of points and the number of colors as input, write a program that computes and prints the minimum size of that makes a double rainbow.
Input
Your program is to read from standard input. The input starts with a line containing two integers and (), where is the number of points in and is the number of colors. Each of the following lines consists of an integer from to , inclusive, and the -th line corresponds to the color of the -th point of from the left.
Output
Your program is to write to standard output. Print exactly one line. The line should contain the minimum size of that makes a double rainbow. If there is no such , print 0.