Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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:
Input: nums = [2,5,4,3]
Output: 4
Explanation:
[2, 5, 4, 3].[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:
[3, 2, 2, 5, 4].[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:
[2, 3, 2].[2] and 1 distinct odd number [3]. Thus, the answer is 3.Constraints:
1 <= nums.length <= 15001 <= nums[i] <= 105Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Segment Tree
[2,5,4,3]
[3,2,2,5,4]
[1,2,3,2]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3719: Longest Balanced Subarray I
class Solution {
public int longestBalanced(int[] nums) {
int n = nums.length;
int ans = 0;
for (int i = 0; i < n; ++i) {
Set<Integer> vis = new HashSet<>();
int[] cnt = new int[2];
for (int j = i; j < n; ++j) {
if (vis.add(nums[j])) {
++cnt[nums[j] & 1];
}
if (cnt[0] == cnt[1]) {
ans = Math.max(ans, j - i + 1);
}
}
}
return ans;
}
}
// Accepted solution for LeetCode #3719: Longest Balanced Subarray I
func longestBalanced(nums []int) (ans int) {
n := len(nums)
for i := 0; i < n; i++ {
vis := map[int]bool{}
cnt := [2]int{}
for j := i; j < n; j++ {
if !vis[nums[j]] {
vis[nums[j]] = true
cnt[nums[j]&1]++
}
if cnt[0] == cnt[1] {
ans = max(ans, j-i+1)
}
}
}
return
}
# Accepted solution for LeetCode #3719: Longest Balanced Subarray I
class Solution:
def longestBalanced(self, nums: List[int]) -> int:
n = len(nums)
ans = 0
for i in range(n):
cnt = [0, 0]
vis = set()
for j in range(i, n):
if nums[j] not in vis:
cnt[nums[j] & 1] += 1
vis.add(nums[j])
if cnt[0] == cnt[1]:
ans = max(ans, j - i + 1)
return ans
// Accepted solution for LeetCode #3719: Longest Balanced Subarray I
use std::collections::HashSet;
impl Solution {
pub fn longest_balanced(nums: Vec<i32>) -> i32 {
let n = nums.len();
let mut ans: i32 = 0;
for i in 0..n {
let mut vis: HashSet<i32> = HashSet::new();
let mut cnt = [0i32; 2];
for j in i..n {
if !vis.contains(&nums[j]) {
vis.insert(nums[j]);
let idx = (nums[j] & 1) as usize;
cnt[idx] += 1;
}
if cnt[0] == cnt[1] {
ans = ans.max((j - i + 1) as i32);
}
}
}
ans
}
}
// Accepted solution for LeetCode #3719: Longest Balanced Subarray I
function longestBalanced(nums: number[]): number {
const n = nums.length;
let ans = 0;
for (let i = 0; i < n; ++i) {
const vis = new Set<number>();
const cnt: number[] = Array(2).fill(0);
for (let j = i; j < n; ++j) {
if (!vis.has(nums[j])) {
vis.add(nums[j]);
++cnt[nums[j] & 1];
}
if (cnt[0] === cnt[1]) {
ans = Math.max(ans, j - i + 1);
}
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
For each of q queries, scan the entire range to compute the aggregate (sum, min, max). Each range scan takes up to O(n) for a full-array query. With q queries: O(n × q) total. Point updates are O(1) but queries dominate.
Building the tree is O(n). Each query or update traverses O(log n) nodes (tree height). For q queries: O(n + q log n) total. Space is O(4n) ≈ O(n) for the tree array. Lazy propagation adds O(1) per node for deferred updates.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.