-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsolution.js
More file actions
29 lines (25 loc) · 709 Bytes
/
solution.js
File metadata and controls
29 lines (25 loc) · 709 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// Solution for LeetCode Problem #2412: LongestSubarrayWithMaximumBitwiseAnd
// Date: 2025-07-30
// Difficulty: Medium
// Language: JavaScript
/**
* @param {number[]} nums
* @param {number} k
* @return {number}
*/
var longestSubarray = function(nums, k) {
let maxLength = 0;
for (let i = 0; i < nums.length; i++) {
let currentAnd = nums[i];
if (currentAnd === k) {
maxLength = Math.max(maxLength, 1);
}
for (let j = i + 1; j < nums.length; j++) {
currentAnd &= nums[j];
if (currentAnd === k) {
maxLength = Math.max(maxLength, j - i + 1);
}
}
}
return maxLength;
};