691. Longest Semi-Repeating Subarray
You are given an integer array nums of length n and an integer k.
A semi‑repeating subarray is a contiguous subarray in which at most k elements repeat (i.e., appear more than once).
Return the length of the longest semi‑repeating subarray in nums.
Example 1:
Input:Â nums = [1,2,3,1,2,3,4], k = 2
Output:Â 6
Explanation:
The longest semi-repeating subarray is [2, 3, 1, 2, 3, 4], which has two repeating elements (2 and 3).
Example 2:
Input:Â nums = [1,1,1,1,1], k = 4
Output:Â 5
Explanation:
The longest semi-repeating subarray is [1, 1, 1, 1, 1], which has only one repeating element (1).
Now Your Turn!
Pick the correct output for the given inputInput:Â nums = [1,1,1,1,1], k = 0
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 <= nums.length <= 105
- 1 <= nums[i] <= 105
- 0 <= k <= nums.length