Skip to content

Practice Length of Longest Subarray With at Most K Frequency

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2958
LeetCode Problem

Length of Longest Subarray With at Most K Frequency

Problem Statement

You are given an integer array nums and an integer k. The frequency of an element x is the number of times it occurs in an array. An array is called good if the frequency of each element in this array is less than or equal to k. Return the length of the longest good subarray of nums. A subarray is a contiguous non-empty sequence of elements within an array.

Source: Length of Longest Subarray With at Most K Frequency on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
nums = [1,2,3,1,2,3,1,2], k = 2
Output
6

# Example 2

Input
nums = [1,2,1,2,1,2,1,2], k = 1
Output
2

# Example 3

Input
nums = [5,5,5,5,5,5,5], k = 4
Output
4

Constraints

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

Related LeetCode Problems

Practice more problems similar to Length of Longest Subarray With at Most K Frequency.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.