Skip to content

Commit 34b185e

Browse files
authored
Create 2419-longest-subarray-with-maximum-bitwise-and.js
1 parent 0780568 commit 34b185e

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
const longestSubarray = function(nums) {
6+
const max = Math.max(...nums)
7+
const arr = []
8+
for(let i = 0; i < nums.length; i++) {
9+
if(nums[i] === max) arr.push(i)
10+
}
11+
let res = 1, cur = 1
12+
for(let i = 1; i < arr.length; i++) {
13+
if(arr[i] - arr[i - 1] === 1) cur++
14+
else {
15+
cur = 1
16+
}
17+
18+
res = Math.max(res, cur)
19+
}
20+
21+
return res
22+
};

0 commit comments

Comments
 (0)