Skip to content

Commit 40c3619

Browse files
Create 1248-count-number-of-nice-subarrays.java
1 parent 8f61c2d commit 40c3619

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

Diff for: java/1248-count-number-of-nice-subarrays.java

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
public int numberOfSubarrays(int[] nums, int k) {
3+
int res = 0, odds = 0;
4+
int l = 0, m = 0;
5+
6+
for(int r = 0; r < nums.length; r++){
7+
if(nums[r] % 2 == 1)
8+
odds += 1;
9+
10+
while(odds > k){
11+
if(nums[l] % 2 == 1)
12+
odds -= 1;
13+
l += 1;
14+
m = l;
15+
}
16+
17+
if(odds == k){
18+
while(nums[m] % 2 != 1){
19+
m += 1;
20+
}
21+
res += m - l + 1;
22+
}
23+
}
24+
return res;
25+
}
26+
}

0 commit comments

Comments
 (0)