Skip to content

Commit 552f63f

Browse files
authored
Create 2871-split-array-into-maximum-number-of-subarrays.js
1 parent fa96187 commit 552f63f

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number}
4+
*/
5+
var maxSubarrays = function(nums) {
6+
let n = nums.length;
7+
let s = nums[0];
8+
for (let i = 1; i < n; i++) s &= nums[i];
9+
if (s > 0) return 1;
10+
11+
let res = 0;
12+
let l = 0;
13+
let cs = nums[l];
14+
for (let r = 0; r < n; r++) {
15+
cs &= nums[r];
16+
if (cs == 0) {
17+
res++;
18+
l = r + 1;
19+
cs = nums[l];
20+
}
21+
}
22+
23+
return res;
24+
};

0 commit comments

Comments
 (0)