Skip to content

Commit 9196d12

Browse files
authored
Create 3755-find-maximum-balanced-xor-subarray-length.js
1 parent b4a7ba3 commit 9196d12

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var maxBalancedSubarray = function(nums) {
6+
const hash = {}
7+
let xor = 0, bal = 0, res = 0
8+
hash[k(0, 0)] = -1
9+
10+
for(let i = 0; i < nums.length; i++) {
11+
const num = nums[i]
12+
xor ^= num
13+
if(num % 2 === 0) bal++
14+
else bal--
15+
if(hash[k(xor, bal)] != null) {
16+
let tmp = i - hash[k(xor, bal)]
17+
res = Math.max(res, tmp)
18+
} else {
19+
hash[k(xor, bal)] = i
20+
}
21+
}
22+
23+
24+
return res
25+
26+
function k(xor, bal) {
27+
return `${xor},${bal}`
28+
}
29+
};

0 commit comments

Comments
 (0)