Length of Longest Subarray With at Most K Frequency - Complete Solution Guide
Length of Longest Subarray With at Most K Frequency is LeetCode problem 2958, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
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. Example 1: Input: nums = [1,2,3,1,2,3,1,2], k = 2 Output: 6 Explanation: The longest possible good subarray is [1,2,3,1,2,3] since the va
Detailed Explanation
The problem asks us to find the length of the longest subarray within a given array `nums` where the frequency of each element in the subarray is less than or equal to a given integer `k`. A 'good' subarray is defined as one where no element appears more than `k` times. We need to return the maximum length of such a 'good' subarray.
Solution Approach
The provided solution uses the sliding window technique. A window is maintained by two pointers, `left` and `right`. The `right` pointer expands the window one element at a time. The frequency of each element in the window is tracked using a hash map. If adding an element to the window causes the frequency of any element to exceed `k`, the `left` pointer is advanced until the frequency constraint is satisfied again. The maximum window length encountered during this process is the answer.
Step-by-Step Algorithm
- Step 1: Initialize a hash map `freq` to store the frequencies of elements within the current window. Initialize `ans` to 0 (to store the maximum length), and `left` to 0 (the starting index of the window).
- Step 2: Iterate through the input array `nums` with the `right` pointer.
- Step 3: For each element at index `right`, increment its frequency in the `freq` hash map.
- Step 4: While the frequency of the current element `nums[right]` is greater than `k`, decrement the frequency of the element at the `left` pointer in the `freq` hash map and increment the `left` pointer.
- Step 5: Update `ans` with the maximum of its current value and the current window length (`right - left + 1`).
- Step 6: After iterating through all elements, return `ans`.
Key Insights
- Insight 1: The core idea is to use a sliding window to maintain a 'good' subarray. We expand the window to the right, adding elements, and shrink it from the left whenever an element's frequency exceeds `k`.
- Insight 2: A hash map (or dictionary) is essential to efficiently track the frequency of each element within the current window.
- Insight 3: The sliding window technique allows us to explore all possible subarrays in O(n) time, where n is the length of the input array.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(n)
Topics
This problem involves: Array, Hash Table, Sliding Window.
Companies
Asked at: Citadel, MakeMyTrip.