Longest Subarray with At Most K Duplicate PairsMedium

Longest Subarray with At Most K Duplicate Pairs

MediumPractice library · +20 XPsliding windowhash maparrays

Given an array A of N integers and an integer K, find the length of the longest subarray A[i...j] such that the total number of pairs of indices (p, q) satisfying i <= p < q <= j and A[p] == A[q] is at most K.

Input

The first line contains two integers N and K, separated by a space. The second line contains N space-separated integers, the elements of the array.

Output

Print a single integer, the length of the longest valid subarray. If N=0, print 0.

Constraints

1 <= N <= 200000 1 <= K <= N Elements of the array are integers between -10^9 and 10^9.

Time limit 1 s (native code; slower languages get more)Memory 256 MB14 hidden tests

Example 1

Input
5 1
1 2 1 3 1
Output
4

For K=1, the longest subarray is [1, 2, 1, 3] (length 4) which has 1 duplicate pair (A[0]=A[2]=1).

Example 2

Input
7 2
1 1 2 2 1 3 3
Output
5

For K=2, the subarray [2, 2, 1, 3, 3] (length 5) has exactly 2 duplicate pairs.

Hints