-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCount_Number_of_Nice_Subarrays.java
More file actions
54 lines (53 loc) · 1.36 KB
/
Count_Number_of_Nice_Subarrays.java
File metadata and controls
54 lines (53 loc) · 1.36 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.util.*;
import java.io.*;
import java.lang.*;
public class Count_Number_of_Nice_Subarrays {
class Solution {
public int numberOfSubarrays(int[] nums, int k) {
int n = nums.length;
int ans = 0;
for(int i = 0 ; i<n ; i++)
{
nums[i] = nums[i] % 2;
}
int[] prefix = new int[n+1];
prefix[0] = 1;
int sum = 0;
for(int e : nums)
{
sum = sum + e;
if(sum>=k)
{
ans = ans + prefix[sum - k];
}
prefix[sum]++;
}
return ans;
}
}
// class Solution {
// public int numberOfSubarrays(int[] nums, int k) {
// int n = nums.length;
// int ans = 0;
// for(int st = 0 ; st<n ; st++)
// {
// for(int end = st ; end<n ; end++)
// {
// int count = 0;
// for(int i = st ; i<=end ; i++)
// {
// if(nums[i]%2 != 0)
// {
// count++;
// }
// }
// if(count == k)
// {
// ans++;
// }
// }
// }
// return ans;
// }
// }
}