#K47082. Shortest Subarray with K Distinct Integers

    ID: 28119 Type: Default 1000ms 256MiB

Shortest Subarray with K Distinct Integers

Shortest Subarray with K Distinct Integers

You are given an array of integers and an integer (K). The task is to find the length of the shortest contiguous subarray that contains at least (K) distinct integers. If no such subarray exists, output -1. This problem can be solved efficiently with a sliding window technique.

inputFormat

The first line contains two integers (N) and (K), representing the number of elements in the array and the required number of distinct integers, respectively. The second line contains (N) space-separated integers (if (N > 0)).

Example Input: 7 3 1 2 2 3 4 2 3

outputFormat

Output a single integer, the length of the shortest contiguous subarray that contains at least (K) distinct integers, or -1 if no such subarray exists.## sample

7 3
1 2 2 3 4 2 3
3