#K84417. Minimum Consecutive Books
Minimum Consecutive Books
Minimum Consecutive Books
Problem Description:
Alyssa loves to read and organize her books by genre. However, she wants to minimize the interruption caused by switching between different genres. To achieve this, she wants to read a contiguous segment of her reading list that contains every distinct genre available. Given the total number of books and the genres of each book, determine the minimum number of consecutive books that include all the different genres present in the entire list.
Formally, let (n) be the number of books and let the array (genres) of length (n) denote the genre of each book. Find the minimum length (L) such that there exists an index (i) with (1 \le i \le n-L+1) for which the subarray (genres[i..i+L-1]) contains every unique genre that appears in (genres). It can be proven that such a segment always exists.
inputFormat
Input Format:
The first line contains two integers (n) and (m), where (n) is the number of books and (m) is the total number of distinct genres.
The second line contains (n) space-separated integers representing the genres of the books.
outputFormat
Output Format:
Print a single integer: the minimum number of consecutive books that contain all the distinct genres.## sample
8 3
1 2 1 3 2 1 2 3
3
</p>