Problem · Array

Minimum Length Good Subarray

Learn this problem
MediumMicrosoft logoMicrosoftINTERNOA
See Microsoft hiring insights

Problem statement

Given an array arr of n positive integers and an integer k, a contiguous subarray is good if it contains at least k distinct integers.

Return the minimum length of a good subarray. If no good subarray exists, return -1.

Function

findMinimumLengthSubarray(arr: int[], k: int) → int

Examples

Example 1

arr = [2,2,1,1,3]k = 3return = 4

The full array and the subarray [2,1,1,3] each contain the three distinct integers 1, 2, and 3. The latter has length 4, and no shorter subarray contains all three values.

Constraints

  • 1 <= arr.length <= 10^5
  • 1 <= arr[i] <= 10^9
  • 1 <= k <= arr.length

More Microsoft problems

drafts saved locally
public int findMinimumLengthSubarray(int[] arr, int k) {
  // Write your code here.
}
arr[2,2,1,1,3]
k3
expected4
checking account