#C4642. Maximum Unique Elements in Subarrays

    ID: 48203 Type: Default 1000ms 256MiB

Maximum Unique Elements in Subarrays

Maximum Unique Elements in Subarrays

You are given an array of n integers and a window size k. Your task is to find the maximum number of distinct elements in any contiguous subarray of length k.

Formally, for an array \(a_1, a_2, \dots, a_n\) and for every contiguous subarray \(S_i = [a_i, a_{i+1}, \dots, a_{i+k-1}]\) (with \(1 \le i \le n-k+1\)), let \(U(S_i)\) represent the number of unique elements in \(S_i\). You need to compute:

$$\max_{1 \le i \le n-k+1} U(S_i)$$

This problem requires an efficient implementation using a sliding window technique.

inputFormat

The input is given via standard input (stdin) and has the following format:

  • The first line contains two integers n and k, where n is the number of elements in the array and k is the size of the subarray.
  • The second line contains n space-separated integers, representing the elements of the array.

outputFormat

Print a single integer to standard output (stdout) – the maximum number of unique elements among all contiguous subarrays of length k.

## sample
7 4
1 2 1 3 4 2 3
4