-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCount_Subarrays_With_Fixed_Bounds.java
More file actions
45 lines (41 loc) · 1.11 KB
/
Count_Subarrays_With_Fixed_Bounds.java
File metadata and controls
45 lines (41 loc) · 1.11 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
import java.util.*;
import java.io.*;
import java.lang.*;
public class Count_Subarrays_With_Fixed_Bounds {
class Solution {
public long countSubarrays(int[] nums, int minK, int maxK) {
int n = nums.length;
long ans = 0;
int left = 0;
int count = 0;
int maxI = -1;
int minI = -1;
for(int right = 0 ; right<n ; right++)
{
if(nums[right] == minK)
{
minI = right;
}
if(nums[right] == maxK)
{
maxI = right;
}
if(nums[right]>maxK || nums[right]<minK)
{
left = right + 1;
}
if(minI == -1 || maxI == -1)
{
continue;
}
count = Math.min(maxI , minI) - left + 1;
if(count<0)
{
count = 0;
}
ans = ans + count;
}
return ans;
}
}
}