Talentd
FleetCode

3755. Find Maximum Balanced XOR Subarray Length

Medium50.8% acceptance

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 <= 105
  • 0 <= nums[i] <= 109

Hints open one at a time

Hint 1
In one pass maintain prefix pxor and prefix diff = (#evens − #odds) and record in a hash‑map the earliest index where each state (pxor,diff) occurs.
Hint 2
At each index, if the current state (pxor,diff) is already in the map, update the max length as the current index minus the recorded index.
19:30
Still stuck?Read the written solution, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def maxBalancedSubarray(self, nums: List[int]) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
[3,1,3,2,0]
Input
[3,2,8,5,4,14,9,15]
Input
[0]
Your code is saved on this device.Solution