Back to Month
MEDIUM 12 Aug 2026 View on LeetCode

2958. Length of Longest Subarray With at Most K Frequency

</> Solution

class Solution {
    public int maxSubarrayLength(int[] nums, int k) {
        Map<Integer, Integer> freq = new HashMap<>();
        int left = 0;
        int maxLen = 0;

        for (int right = 0; right < nums.length; right++) {
            freq.put(nums[right], freq.getOrDefault(nums[right], 0) + 1);

            while (freq.get(nums[right]) > k) {
                freq.put(nums[left], freq.get(nums[left]) - 1);
                left++;
            }

            maxLen = Math.max(maxLen, right - left + 1);
        }

        return maxLen;
    }
}

TIME COMPLEXITY

O(n)

SPACE COMPLEXITY

O(n)

TOPICS

Array Hash Table Sliding Window Two Pointers