-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaximum occurred integer
63 lines (47 loc) · 1.55 KB
/
Maximum occurred integer
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
55
56
57
58
59
60
61
62
63
//Maximum occurred integer
import java.io.*;
import java.lang.*;
import java.util.*;
class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine().trim());
while(t-- > 0) {
int n = Integer.parseInt(br.readLine().trim());
int l[] = new int[n];
int r[] = new int[n];
String inputLine[] = br.readLine().trim().split(" ");
for(int i = 0; i < n; i++) {
l[i] = Integer.parseInt(inputLine[i]);
}
inputLine = br.readLine().trim().split(" ");
int maxx = Integer.MIN_VALUE;
for(int i = 0; i < n; i++) {
r[i] = Integer.parseInt(inputLine[i]);
if(r[i] > maxx) {
maxx = r[i];
}
}
Solution obj = new Solution();
System.out.println(obj.maxOccured(n, l, r, maxx));
}
}
}
class Solution {
public static int maxOccured(int n, int l[], int r[], int maxx) {
int arr[] = new int[maxx + 2];
for(int i = 0; i < n ;i++) {
arr[l[i]]++;
arr[r[i] + 1]--;
}
int max = arr[0], res = 0;
for(int i = 1; i < maxx + 1; i++) {
arr[i] += arr[i - 1];
if(max < arr[i]) {
max = arr[i];
res = i;
}
}
return res;
}
}