2958. Length of Longest Subarray With at Most K Frequency LeetCode Solution
In this guide, you will get 2958. Length of Longest Subarray With at Most K Frequency LeetCode Solution with the best time and space complexity. The solution to Length of Longest Subarray With at Most K Frequency problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Length of Longest Subarray With at Most K Frequency solution in C++
- Length of Longest Subarray With at Most K Frequency solution in Java
- Length of Longest Subarray With at Most K Frequency solution in Python
- Additional Resources
Problem Statement of Length of Longest Subarray With at Most K Frequency
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 values 1, 2, and 3 occur at most twice in this subarray. Note that the subarrays [2,3,1,2,3,1] and [3,1,2,3,1,2] are also good.
It can be shown that there are no good subarrays with length more than 6.
Example 2:
Input: nums = [1,2,1,2,1,2,1,2], k = 1
Output: 2
Explanation: The longest possible good subarray is [1,2] since the values 1 and 2 occur at most once in this subarray. Note that the subarray [2,1] is also good.
It can be shown that there are no good subarrays with length more than 2.
Example 3:
Input: nums = [5,5,5,5,5,5,5], k = 4
Output: 4
Explanation: The longest possible good subarray is [5,5,5,5] since the value 5 occurs 4 times in this subarray.
It can be shown that there are no good subarrays with length more than 4.
Constraints:
1 <= nums.length <= 105
1 <= nums[i] <= 109
1 <= k <= nums.length
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
2958. Length of Longest Subarray With at Most K Frequency LeetCode Solution in C++
class Solution {
public:
int maxSubarrayLength(vector<int>& nums, int k) {
int ans = 0;
unordered_map<int, int> count;
for (int l = 0, r = 0; r < nums.size(); ++r) {
++count[nums[r]];
while (count[nums[r]] == k + 1)
--count[nums[l++]];
ans = max(ans, r - l + 1);
}
return ans;
}
};
/* code provided by PROGIEZ */
2958. Length of Longest Subarray With at Most K Frequency LeetCode Solution in Java
class Solution {
public int maxSubarrayLength(int[] nums, int k) {
int ans = 0;
Map<Integer, Integer> count = new HashMap<>();
for (int l = 0, r = 0; r < nums.length; ++r) {
count.merge(nums[r], 1, Integer::sum);
while (count.get(nums[r]) == k + 1)
count.merge(nums[l++], -1, Integer::sum);
ans = Math.max(ans, r - l + 1);
}
return ans;
}
}
// code provided by PROGIEZ
2958. Length of Longest Subarray With at Most K Frequency LeetCode Solution in Python
class Solution:
def maxSubarrayLength(self, nums: list[int], k: int) -> int:
ans = 0
count = collections.Counter()
l = 0
for r, num in enumerate(nums):
count[num] += 1
while count[num] == k + 1:
count[nums[l]] -= 1
l += 1
ans = max(ans, r - l + 1)
return ans
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.