-
Notifications
You must be signed in to change notification settings - Fork 0
/
permutations.java
48 lines (39 loc) · 1.38 KB
/
permutations.java
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
import java.util.*;
// time = O(1), space = O(n)
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int q = sc.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
int[] prefixMax = new int[n];
int[] suffixMax = new int[n];
// Compute prefixMax
prefixMax[0] = a[0];
for (int i = 1; i < n; i++) {
prefixMax[i] = Math.max(prefixMax[i-1], a[i]);
}
// Compute suffixMax
suffixMax[n-1] = a[n-1];
for (int i = n-2; i >= 0; i--) {
suffixMax[i] = Math.max(suffixMax[i+1], a[i]);
}
// Process each query
for (int qi = 0; qi < q; qi++) {
int l = sc.nextInt() - 1; // Convert to 0-based index
int r = sc.nextInt() - 1; // Convert to 0-based index
int maxOutsideSubarray = -1;
if (l > 0) {
maxOutsideSubarray = Math.max(maxOutsideSubarray, prefixMax[l-1]);
}
if (r < n-1) {
maxOutsideSubarray = Math.max(maxOutsideSubarray, suffixMax[r+1]);
}
System.out.println(maxOutsideSubarray);
}
sc.close();
}
}