#K76857. Longest Subarray with K Distinct Integers

    ID: 34735 Type: Default 1000ms 256MiB

Longest Subarray with K Distinct Integers

Longest Subarray with K Distinct Integers

You are given an array of integers and an integer \(k\). Your task is to find the length of the longest contiguous subarray that contains at most \(k\) distinct integers.

For example, given the array [1, 2, 1, 2, 3] and \(k = 2\), the longest subarray with at most 2 distinct integers is [1, 2, 1, 2] with length 4.

If \(k = 0\) or the array is empty, the result is 0.

inputFormat

The input is read from standard input (stdin) and consists of two lines:

  1. The first line contains an integer \(k\), representing the maximum allowed number of distinct integers in a subarray.
  2. The second line contains space-separated integers representing the array. This line may be empty if the array is empty.

outputFormat

Output a single integer to standard output (stdout), which is the length of the longest contiguous subarray with at most \(k\) distinct integers.

## sample
2
1 2 1 2 3
4