↓Skip to main content
  1. LeetCode/

LeetCode 3719: Longest Balanced Subarray I

·2 mins· ·
LeetCode Medium Hash-Table Prefix-Sum Segment-Tree Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: medium First Attempt: 2026-02-10

  • Submissions: Accepted (first try)

Approach
#

The condition is “number of distinct evens = number of distinct odds”. Distinct counts cannot be subtracted like prefix sums, but n <= 1500, so O(n²) is enough:

  • Fix the left end i and extend the right end j.
  • Two sets hold the odds and evens seen; each new number updates one set in O(1).
  • When the sets have equal size, update the answer.

With a much larger n (version II of this problem), O(n²) is too slow and a more advanced data structure is needed.

My Solution
#

class Solution:
    def longestBalanced(self, nums: List[int]) -> int:
        n, best = len(nums), 0
        for i in range(n):
            odd, even = set(), set()
            for j in range(i, n):
                (odd if nums[j] % 2 else even).add(nums[j])
                if len(odd) == len(even):
                    best = max(best, j - i + 1)
        return best

Complexity
#

Time O(n²), space O(n).

Mistakes to Avoid
#

  • Count distinct values; repeats must not count twice, hence sets.
  • Update incrementally while extending; do not recompute the whole subarray.

Interview Explanation
#

With n only 1500, I fix the left end and extend right, keeping the distinct odds and evens seen in two sets with O(1) updates, and record the length whenever their sizes match. Time O(n²).

Related

LeetCode 930: Binary Subarrays With Sum
·2 mins
LeetCode Medium Sliding-Window Prefix-Sum Hash-Table Array
LeetCode 930 note: “sum exactly goal” = “sum at most goal” − “sum at most goal - 1”; or prefix sums with a hash map
LeetCode 2875: Minimum Size Subarray in Infinite Array
·2 mins
LeetCode Medium Sliding-Window Prefix-Sum Hash-Table Array
LeetCode 2875 note: strip target // sum full cycles; the remainder is smaller than one cycle, so a sliding window over two copies of nums always finds it
LeetCode 1248: Count Number of Nice Subarrays
·3 mins
LeetCode Medium Sliding-Window Prefix-Sum Hash-Table Array
LeetCode 1248 note: “exactly k odd numbers” is not monotonic, so compute “at most k” minus “at most k - 1”; or use prefix sums with a hash map
LeetCode 3810: Minimum Operations to Reach Target Array
·2 mins
LeetCode Medium Greedy Hash-Table Array
LeetCode 3810 note: one operation fixes every position holding a value, so the answer is the number of distinct values at mismatched positions
LeetCode 1838: Frequency of the Most Frequent Element
·2 mins
LeetCode Medium Sliding-Window Sorting Greedy Prefix-Sum Binary-Search Array
LeetCode 1838 note: after sorting, raising the whole window to nums[right] costs nums[right] × length − window sum; shrink the left while that exceeds k
LeetCode 2271: Maximum White Tiles Covered by a Carpet
·2 mins
LeetCode Medium Sliding-Window Greedy Sorting Prefix-Sum Binary-Search Array
LeetCode 2271 note: starting the carpet at some segment’s start is never worse; after sorting, two pointers sum the fully covered segments and count the last one partially