Skip to content

Commit 3b65d37

Browse files
committed
Runtime: 12 ms (Top 87.36%) | Memory: 75.4 MB (Top 41.15%)
1 parent a4580f1 commit 3b65d37

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

scripts/algorithms/C/Count Number of Nice Subarrays/Count Number of Nice Subarrays.java

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
1+
// Runtime: 12 ms (Top 87.36%) | Memory: 75.4 MB (Top 41.15%)
12
class Solution {
23
public int numberOfSubarrays(int[] nums, int k) {
34
int i = 0;
45
int j = 0;
56
int odd = 0;
67
int result = 0;
78
int temp = 0;
8-
9-
10-
/*
9+
10+
/*
1111
Approach : two pointer + sliding window technique
12-
12+
1313
step 1 : we have fix i and moving j until our count of odd numbers == k
1414
step 2 : when(odd == count) we are counting every possible subarray by reducing the size of subarray from i
15-
15+
1616
why temp?
1717
from reducing the size of subarray we will count all the possible subarray from between i and j
1818
but when i encounter a odd element the odd count will reduce and that while will stop executing
19-
19+
2020
now there are two possible cases
2121
1.The leftover elements have a odd number
2222
2.The leftover elements do not have any odd number
23-
24-
1. if our leftover elements have a odd number
23+
24+
1. if our leftover elements have a odd number
2525
then we cannot include our old possible subarrays into new possible subarrays because now new window for having odd == k is formed
2626
that's why temp = 0;
27-
27+
2828
2. if out leftover elements do not have any odd element left
2929
then our leftover elements must also take in consideration becuase they will also contribute in forming subarrays
3030
*/
@@ -45,8 +45,8 @@ public int numberOfSubarrays(int[] nums, int k) {
4545
//so include them
4646
result += temp;
4747
j++;
48-
48+
4949
}
5050
return result;
5151
}
52-
}
52+
}

0 commit comments

Comments
 (0)