Longest Balanced Subarray II
You receive an integer array nums .
A subarray is called balanced if the count of distinct even numbers in the subarray is equal to the count of distinct odd numbers.
Produce the length of the longest balanced subarray.
Input: nums = [2,5,4,3]
Output: 4
Explanation: For this case,
-
the longest balanced subarray is [2, 5, 4, 3] .
-
It has 2 distinct even numbers [2, 4] and 2 distinct odd numbers [5, 3] . Thus, the answer is 4.
Input: nums = [3,2,2,5,4]
Output: 5
Explanation: For this case,
-
the longest balanced subarray is [3, 2, 2, 5, 4] .
-
It has 2 distinct even numbers [2, 4] and 2 distinct odd numbers [3, 5] . Thus, the answer is 5.
Input: nums = [1,2,3,2]
Output: 3
Explanation: For this case,
-
the longest balanced subarray is [2, 3, 2] .
-
It has 1 distinct even number [2] and 1 distinct odd number [3] . Thus, the answer is 3.
Constraints
1 <= nums.length <= 1000001 <= nums[i] <= 100000
Did this problem appear in one of your interviews?
Responses stay private.
