Length of Longest Subarray With at Most K Frequency
You are given an integer array `nums` and an integer `k`. A subarray is good if every value in it appears at most `k` times. Return the length of the longest good subarray of `nums`.
30:00
Length of Longest Subarray With at Most K Frequency
You are given an integer array nums and an integer k. A subarray is good if every value in it appears at most k times. Return the length of the longest good subarray of nums.
Example 1:
Input: {"nums":[1,2,3,1,2,3,1,2],"k":2}
Output: 6
Constraints:
1≤nums.length≤105
1≤nums[i]≤109
1≤k≤nums.length
Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result
Run code to see results.
Input
arr ={"nums":[1,2,3,1,2,3,1,2],"k":2}
Grow a window to the right and count each value in it. No value may appear more than k = 2 times; when one does, shrink from the left.