Lollipop
InterviewTime limit4sMemory limit1024 MB
Find the shortest contiguous piece in which some flavor appears at least three times, or report NIE if no such piece exists.
- Level
Medium5 of 10
- Topics
- Sliding window, Hash map
- Solved
- No attempts yet
Problem
Bajtek walked into a candy shop. A long lollipop made of parts of equal length caught his eye. Each part has a certain flavor.

Bajtek wants to pick a contiguous piece of this lollipop. To do so, he may break off some number of parts (possibly zero) from the left end and from the right end. The figure below shows several ways to pick pieces from the lollipop above. The pieces in the figure are marked with black frames.

Bajtek considers a piece tasty if it contains at least three parts with the same flavor. He does not care which flavor appears three times, or how many different flavors the piece has. All pieces in the figure above are tasty.
Bajtek does not want to overeat, so he wants the shortest tasty piece, meaning the piece made of the fewest parts. Write a program that finds the length of the shortest tasty piece of the lollipop described in the input, or reports that no tasty piece can be picked.
Input
The first line contains a natural number (), the number of parts of the lollipop. The second line, which is also the last line, describes the lollipop with natural numbers (), the flavors of the parts from left to right. Different flavors are written as different numbers.
Output
Print one integer, the minimum number of parts in a tasty piece. If no tasty piece can be picked, print only the word NIE.