2401.js
/** ------------------------------------------------------------------------------
*
* 2401. Longest Nice Subarray
* Topics: Sliding Window, Bit Manipulation
* https://leetcode.com/problems/longest-nice-subarray/description/?envType=daily-question&envId=2025-03-18
*
------------------------------------------------------------------------------ */
/**
* @param {number[]} nums
* @return {number}
*/
var longestNiceSubarray = function (nums) {
let left = 0
let bitMask = 0
let max = 1
for (let right = 0; right < nums.length; right++) {
while ((bitMask & nums[right]) !== 0) {
bitMask ^= nums[left]
left++
}
bitMask |= nums[right]
max = Math.max(max, right - left + 1)
}
return max
}
console.log(longestNiceSubarray([1, 3, 8, 48, 10]))