Given an integer array nums, return the length of the longest contiguous subarray that contains at most two distinct values.
Examples
Example 1
Input: nums = [1,2,1]
Output: 3
Explanation: The entire array contains only the two distinct values 1 and 2.
Example 2
Input: nums = [0,1,2,2]
Output: 3
Explanation: The subarray [1,2,2] contains two distinct values. Any subarray including both the 0 and a 2 contains three distinct values.
Example 3
Input: nums = [1,2,3,2,2]
Output: 4
Explanation: The subarray [2,3,2,2] contains only the two distinct values 2 and 3.
Constraints
1≤nums.length≤105
0≤nums[i]<nums.length
Loading editor...
Log in to run your code. Your draft is saved in this browser.Log inSign up