Longest Balanced Subarray II

You are given an integer array nums.

A subarray is called balanced if the number of distinct even numbers in the subarray is equal to the number of distinct odd numbers.

Return the length of the longest balanced subarray.

Example 1
Inputnums = [2,5,4,3]
Output4
The longest balanced subarray is [2, 5, 4, 3], which has 2 distinct even numbers [2, 4] and 2 distinct odd numbers [5, 3].
Example 2
Inputnums = [3,2,2,5,4]
Output5
The longest balanced subarray is [3, 2, 2, 5, 4], which has 2 distinct even numbers [2, 4] and 2 distinct odd numbers [3, 5].

Constraints

  • 1 <= nums.length <= 10^5
  • 1 <= nums[i] <= 10^5

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate