#K73952. Smallest Window with K Distinct Integers

    ID: 34089 Type: Default 1000ms 256MiB

Smallest Window with K Distinct Integers

Smallest Window with K Distinct Integers

Given an array of integers and an integer K, find the smallest contiguous subarray (window) that contains at least K distinct integers. In other words, for a window defined by indices L and R, it is valid if $$\left|{ arr[i] : L \le i \le R }\right| \ge K.$$ If such a window exists, output the starting and ending indices (0-indexed) of the smallest window. If there are multiple valid answers, any one is acceptable. If no such window exists, output -1. The solution is expected to be efficient, ideally running in O(n) time.

inputFormat

The input consists of three lines.\nThe first line contains an integer N, representing the number of elements in the array.\nThe second line contains N space-separated integers, representing the array elements.\nThe third line contains an integer K.

outputFormat

If a valid subarray exists, output two integers representing the starting and ending indices (0-indexed) of the smallest window, separated by a space in a single line. If no valid window exists, output -1.## sample

7
1 2 1 3 1 4 1
3
1 3