-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoundedMaximum.java
More file actions
36 lines (36 loc) · 1016 Bytes
/
BoundedMaximum.java
File metadata and controls
36 lines (36 loc) · 1016 Bytes
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
import java.util.*;
public class BoundedMaximum {
public static int validSubarray(int[] arr, int l , int r){
int ans = 0;
int lgei = 0;
int prevValid = 0;
for (int ep = 0; ep < arr.length; ep++) {
if (arr[ep] > r) {
ans += 0;
lgei = ep+1;
prevValid = 0;
}
else if (arr[ep] >= l && arr[ep] <= r) {
ans += (ep-lgei+1);
prevValid = ep-lgei+1;
}
else {
ans += prevValid;
}
}
return ans;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
}
int l = sc.nextInt();
int r = sc.nextInt();
int ans = validSubarray(arr,l,r);
System.out.println(ans);
sc.close();
}
}