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.
Given an integer array nums, return the length of the longest subarray that has a bitwise XOR of zero and contains an equal number of even and odd numbers. If no such subarray exists, return 0.
Example 1:
Input: nums = [3,1,3,2,0]
Output: 4
Explanation:
The subarray [1, 3, 2, 0] has bitwise XOR 1 XOR 3 XOR 2 XOR 0 = 0 and contains 2 even and 2 odd numbers.
Example 2:
Input: nums = [3,2,8,5,4,14,9,15]
Output: 8
Explanation:
The whole array has bitwise XOR 0 and contains 4 even and 4 odd numbers.
Example 3:
Input: nums = [0]
Output: 0
Explanation:
No non-empty subarray satisfies both conditions.
Constraints:
1 <= nums.length <= 1050 <= nums[i] <= 109Problem summary: Given an integer array nums, return the length of the longest subarray that has a bitwise XOR of zero and contains an equal number of even and odd numbers. If no such subarray exists, return 0.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Bit Manipulation
[3,1,3,2,0]
[3,2,8,5,4,14,9,15]
[0]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3755: Find Maximum Balanced XOR Subarray Length
class Solution {
public int maxBalancedSubarray(int[] nums) {
Map<Long, Integer> d = new HashMap<>();
int ans = 0;
int a = 0, b = nums.length;
d.put((long) b, -1);
for (int i = 0; i < nums.length; ++i) {
a ^= nums[i];
b += nums[i] % 2 == 0 ? 1 : -1;
long key = (1L * a << 32) | b;
if (d.containsKey(key)) {
ans = Math.max(ans, i - d.get(key));
} else {
d.put(key, i);
}
}
return ans;
}
}
// Accepted solution for LeetCode #3755: Find Maximum Balanced XOR Subarray Length
func maxBalancedSubarray(nums []int) (ans int) {
d := map[int64]int{}
a := 0
b := len(nums)
d[int64(b)] = -1
for i, x := range nums {
a ^= x
if x%2 == 0 {
b++
} else {
b--
}
key := int64(a)<<32 | int64(b)
if j, ok := d[key]; ok {
ans = max(ans, i-j)
} else {
d[key] = i
}
}
return
}
# Accepted solution for LeetCode #3755: Find Maximum Balanced XOR Subarray Length
class Solution:
def maxBalancedSubarray(self, nums: List[int]) -> int:
d = {(0, 0): -1}
a = b = 0
ans = 0
for i, x in enumerate(nums):
a ^= x
b += 1 if x % 2 == 0 else -1
if (a, b) in d:
ans = max(ans, i - d[(a, b)])
else:
d[(a, b)] = i
return ans
// Accepted solution for LeetCode #3755: Find Maximum Balanced XOR Subarray Length
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3755: Find Maximum Balanced XOR Subarray Length
// class Solution {
// public int maxBalancedSubarray(int[] nums) {
// Map<Long, Integer> d = new HashMap<>();
// int ans = 0;
// int a = 0, b = nums.length;
// d.put((long) b, -1);
// for (int i = 0; i < nums.length; ++i) {
// a ^= nums[i];
// b += nums[i] % 2 == 0 ? 1 : -1;
// long key = (1L * a << 32) | b;
// if (d.containsKey(key)) {
// ans = Math.max(ans, i - d.get(key));
// } else {
// d.put(key, i);
// }
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #3755: Find Maximum Balanced XOR Subarray Length
function maxBalancedSubarray(nums: number[]): number {
const d = new Map<bigint, number>();
let ans = 0;
let a = 0;
let b = nums.length;
d.set(BigInt(b), -1);
for (let i = 0; i < nums.length; ++i) {
a ^= nums[i];
b += nums[i] % 2 === 0 ? 1 : -1;
const key = (BigInt(a) << 32n) | BigInt(b);
if (d.has(key)) {
ans = Math.max(ans, i - d.get(key)!);
} else {
d.set(key, i);
}
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Sort the array in O(n log n), then scan for the missing or unique element by comparing adjacent pairs. Sorting requires O(n) auxiliary space (or O(1) with in-place sort but O(n log n) time remains). The sort step dominates.
Bitwise operations (AND, OR, XOR, shifts) are O(1) per operation on fixed-width integers. A single pass through the input with bit operations gives O(n) time. The key insight: XOR of a number with itself is 0, which eliminates duplicates without extra space.
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.