diff --git a/src/main/java/g3701_3800/s3755_find_maximum_balanced_xor_subarray_length/Solution.java b/src/main/java/g3701_3800/s3755_find_maximum_balanced_xor_subarray_length/Solution.java new file mode 100644 index 000000000..18833f611 --- /dev/null +++ b/src/main/java/g3701_3800/s3755_find_maximum_balanced_xor_subarray_length/Solution.java @@ -0,0 +1,30 @@ +package g3701_3800.s3755_find_maximum_balanced_xor_subarray_length; + +// #Medium #Array #Hash_Table #Bit_Manipulation #Prefix_Sum #Senior #Weekly_Contest_477 +// #2026_04_25_Time_91_ms_(98.50%)_Space_256.08_MB_(93.23%) + +import java.util.HashMap; +import java.util.Map; + +public class Solution { + public int maxBalancedSubarray(int[] nums) { + int n = nums.length; + int ans = 0; + int xor = 0; + int diff = n; + Map pos = new HashMap<>(n + 1, 1); + pos.put((long) xor << 20 | diff, -1); + for (int i = 0; i < n; i++) { + xor ^= nums[i]; + diff += nums[i] % 2 != 0 ? 1 : -1; + long key = (long) xor << 20 | diff; + Integer j = pos.get(key); + if (j != null) { + ans = Math.max(ans, i - j); + } else { + pos.put(key, i); + } + } + return ans; + } +} diff --git a/src/main/java/g3701_3800/s3755_find_maximum_balanced_xor_subarray_length/readme.md b/src/main/java/g3701_3800/s3755_find_maximum_balanced_xor_subarray_length/readme.md new file mode 100644 index 000000000..6ca14238e --- /dev/null +++ b/src/main/java/g3701_3800/s3755_find_maximum_balanced_xor_subarray_length/readme.md @@ -0,0 +1,40 @@ +3755\. Find Maximum Balanced XOR Subarray Length + +Medium + +Given an integer array `nums`, return the **length** of the **longest **non-empty subarrays**** 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 \ No newline at end of file diff --git a/src/test/java/g3701_3800/s3755_find_maximum_balanced_xor_subarray_length/SolutionTest.java b/src/test/java/g3701_3800/s3755_find_maximum_balanced_xor_subarray_length/SolutionTest.java new file mode 100644 index 000000000..eac7b7c92 --- /dev/null +++ b/src/test/java/g3701_3800/s3755_find_maximum_balanced_xor_subarray_length/SolutionTest.java @@ -0,0 +1,20 @@ +package g3701_3800.s3755_find_maximum_balanced_xor_subarray_length; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.junit.jupiter.api.Test; + +class SolutionTest { + @Test + void maxBalancedSubarray() { + assertThat(new Solution().maxBalancedSubarray(new int[] {3, 1, 3, 2, 0}), equalTo(4)); + } + + @Test + void maxBalancedSubarray2() { + assertThat( + new Solution().maxBalancedSubarray(new int[] {3, 2, 8, 5, 4, 14, 9, 15}), + equalTo(8)); + } +}