Loading...
Given an integer array nums, return the length of the longest contiguous subarray that contains at most two distinct values.
Input: nums = [1,2,1]
Output: 3
Explanation: The entire array contains only the two distinct values 1 and 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.
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.
nums.length ≤105nums[i] < nums.lengthClick "Run" to test with sample cases or "Submit" to run all tests.