coding

3719. Longest Balanced Subarray I

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.

·2 min read·489 words
Share

Description

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.

What is a subarray?

A subarray is a contiguous non-empty sequence of elements within an array.

Example 1:

Input: nums = [2,5,4,3]
Output: 4
Explanation:
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.

Example 2:

Input: nums = [3,2,2,5,4]
Output: 5
Explanation:
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.

Example 3:

Input: nums = [1,2,3,2]
Output: 3
Explanation:
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.

Approach

Now although in the question the constraints are pretty light and we can examine subarrays starting from each index, but recomputing each distinct count for every subarray would be very inefficient. Our approach could be,

  1. We can fix an index, lets say i for the subarray.
  2. We can now initialize two frequency maps -> (1) one for even numbers (2) one for odd numbers.
  3. Now will also keep counters to check for distinct even and odd values.
  4. We will now expand the subarray by moving the ending index j from i to the end of the array.
  5. For each new element nums[j]: If it is even, update the even frequency map and increase the distinct even count if it appears for the first time. If it is odd, update the odd frequency map and increase the distinct odd count if it appears for the first time.
  6. Now after each expansion, check whether the number of distinct even values equals the number of distinct odd values. If yes, update the maximum length of the balanced subarray.

We will repeat this process for all possible starting indices and return the maximum length found.

This avoids reprocessing entire subarrays and ensures the solution runs efficiently.

Code

func longestBalanced(nums []int) int {
    n:=len(nums)
    ans:=0

    for i:=0;i<n;i++{
        evenMap := make(map[int]int)
        oddMap := make(map[int]int)
        evenDistinct, oddDistinct := 0,0

        for j:=i;j<n;j++{
            a:=nums[j]

            if a%2==0{
                if evenMap[a]==0{
                    evenDistinct++
                }
                evenMap[a]++
            } else {
                if oddMap[a]==0{
                    oddDistinct++
                }
                oddMap[a]++
            }
            if evenDistinct == oddDistinct{
                if j-i+1 > ans {
                    ans=j-i+1
                }
            }
        }
    }
    return ans
}

Complexity Analysis

  • Time Complexity: O(n^2) -> 2 nested loops to expand subarrays with each step updating count in constant time.
  • Space Complexity: O(n) -> Frequency maps can store up to n distinct numbers in the worst case scenario.