NewThe Learning module is now liveSee what's new

(This question is a variation of the LeetCode question 992. Subarrays with K Different Integers. If you haven't completed that question yet, it is recommended to solve it first.)

Given an integer array arr and an integer k, a subarray is called good if it contains at least k distinct integers. Return the length of the shortest good subarray. If no good subarray exists, return -1.

Constraints:

  • 1 ≤ arr.length ≤ 10510^5
  • 1 ≤ arr[i] ≤ 10510^5
  • 1 ≤ k ≤ arr.length
Test Cases
Input
Output
Expected