3719. Longest Balanced Subarray I

  • Array
  • Hash Table
  • Brute Force
TIME
O(n²)
SPACE
O(n)
problem on leetcode ↗source on github ↗

Intuition

For each starting index, expand the subarray while tracking distinct even and odd numbers. When counts are equal, we have a balanced subarray.

Solution

  1. Enumerate all subarrays: For each starting index i, expand j from i to n-1.

  2. Track distinct evens and odds: Use two sets to track distinct even and odd numbers in current subarray.

  3. Check balance condition: When len(even) == len(odd), update max length.

  4. Return maximum: The longest balanced subarray found.

Why Brute Force Works

  • Constraint is small: n <= 1500
  • O(n²) with O(n) set operations per subarray is acceptable
  • Total: O(n²) iterations, each with O(1) amortized set operations

Complexity

  • Time: O(n²), enumerate all subarrays
  • Space: O(n), sets can hold up to n distinct elements

Edge Cases

  • All even or all odd → return 0 (no balanced subarray possible)
  • Single element → return 0
  • Alternating even/odd → likely the entire array is balanced

Code

from typing import List


class Solution:
    def longestBalanced(self, nums: List[int]) -> int:
        """
        3719. Longest Balanced Subarray I
        Time: O(n²)
        Space: O(n)
        """
        n = len(nums)
        ans = 0

        for i in range(n):
            even = set()
            odd = set()

            for j in range(i, n):
                if nums[j] % 2 == 0:
                    even.add(nums[j])
                else:
                    odd.add(nums[j])

                if len(even) == len(odd):
                    ans = max(ans, j - i + 1)

        return ans